在 SparkR DataFrame 中一次重命名多个列
Rename multiple columns at once in SparkR DataFrame
如何一次重命名 SparkR DataFrame
中的多个列,而不是多次调用 withColumnRenamed()
?例如,假设我想将下面 DataFrame
中的列重命名为 name
和 birthdays
,如果不调用 withColumnRenamed()
两次,我该怎么做?
team <- data.frame(name = c("Thomas", "Bill", "George", "Randall"),
surname = c("Johnson", "Clark", "Williams", "Yosimite"),
dates = c('2017-01-05', '2017-02-23', '2017-03-16', '2017-04-08'))
team <- createDataFrame(team)
team <- withColumnRenamed(team, 'surname', 'name')
team <- withColumnRenamed(team, 'dates', 'birthdays')
此处适用标准 R 方法 - 您只需重新分配 colnames
:
colnames(team) <- c("name", "name", "birthdays")
team
SparkDataFrame[name:string, name:string, birthdays:string]
如果您知道顺序,您可以跳过完整列表并且
colnames(team)[colnames(team) %in% c("surname", "dates")] <- c("name", "birthdays")
尽管如此,您可能希望避免重名。
如何一次重命名 SparkR DataFrame
中的多个列,而不是多次调用 withColumnRenamed()
?例如,假设我想将下面 DataFrame
中的列重命名为 name
和 birthdays
,如果不调用 withColumnRenamed()
两次,我该怎么做?
team <- data.frame(name = c("Thomas", "Bill", "George", "Randall"),
surname = c("Johnson", "Clark", "Williams", "Yosimite"),
dates = c('2017-01-05', '2017-02-23', '2017-03-16', '2017-04-08'))
team <- createDataFrame(team)
team <- withColumnRenamed(team, 'surname', 'name')
team <- withColumnRenamed(team, 'dates', 'birthdays')
此处适用标准 R 方法 - 您只需重新分配 colnames
:
colnames(team) <- c("name", "name", "birthdays")
team
SparkDataFrame[name:string, name:string, birthdays:string]
如果您知道顺序,您可以跳过完整列表并且
colnames(team)[colnames(team) %in% c("surname", "dates")] <- c("name", "birthdays")
尽管如此,您可能希望避免重名。