【问题标题】:R - How to rename every nth column with "name_x" where x=1 and increases by 1 for each column?R - 如何用“name_x”重命名每个第 n 列,其中 x=1 并为每列增加 1?
【发布时间】:2020-04-16 04:22:29
【问题描述】:

我有一个数据集,其中列的名称非常混乱,我想简化它们。下面的示例数据:

structure(list(MemberID = 1L, This.was.the.first.question = "ABC", 
    This.was.the.first.date = 1012018L, This.was.the.first.city = "New York", 
    This.was.the.second.question = "XYZ", This.was.the.second.date = 11052018L, 
    This.was.the.second.city = "Boston"), .Names = c("MemberID", 
"This.was.the.first.question", "This.was.the.first.date", "This.was.the.first.city", 
"This.was.the.second.question", "This.was.the.second.date", "This.was.the.second.city"
), class = "data.frame", row.names = c(NA, -1L)) 
MemberID 这是第一个问题 这是第一个约会 这是第一个城市 这是第二个问题 这是第二个约会 这是第二个城市 1 ABC 1012018 纽约 XYZ 11052018 波士顿

这就是我希望列的样子:

会员 ID Question_1 Date_1 City_1 Question_2 Date_2 City_2

所以基本上列名是相同的,但每第三列数字增加 1。我该怎么做?虽然这个示例数据集很小,但我的真实数据集要大得多,我想通过列索引和迭代来学习如何做到这一点。

【问题讨论】:

  • 假设您有 n 个“集合”,并且您的数据框称为 df;尝试 n=2; colnames(df)

标签: r loops rename


【解决方案1】:

一个更简单的选择是删除除最后一个单词之外的子字符串并使用make.unique

names(df1)[-1] <- make.unique(sub(".*\\.", "", names(df1)[-1]), sep="_")
names(df1)
#[1] "MemberID"   "question"   "date"       "city"       "question_1" "date_1"     "city_1"  

或者如果我们需要准确的输出,使用sub 提取最后一个单词并使用ave 根据重复名称创建序列

v1 <-  sub(".*\\.(\\w)", "\\U\\1", names(df1)[-1], perl = TRUE)
names(df1)[-1] <- paste(v1, ave(v1, v1, FUN = seq_along), sep="_")
names(df1)
#[1] "MemberID"   "Question_1" "Date_1"     "City_1"  
#[5]   "Question_2" "Date_2"     "City_2"    

【讨论】:

    【解决方案2】:
    #
    # create vector of question name triplets
    
    theList <- c("question_","date_","city_")
    
    # create enough headings for 10 questions
    questions <- rep(theList,10)
    
    idNumbers <- 1:length(questions)
    
    library(numbers)
    
    # use mod function to group ids into triplets
    idNumbers <- as.character(ifelse(mod(idNumbers,3)>0,floor(idNumbers/3)+1,floor(idNumbers/3)))
    
    # concatenate question stems with numbers and add MemberID column at start of vector
    questionHeaders <- c("MemberID",paste0(questions,idNumbers))
    head(questionHeaders)
    

    ...和输出:

    [1] "MemberID"   "question_1" "date_1"     "city_1"     "question_2" "date_2" 
    

    使用colnames()names() 函数将此向量指定为数据框的列名。

    正如 OP 上的 cmets 中所述,问题 ID 编号可以通过使用 rep() 中的 each= 参数生成,无需使用 mod() 函数。

    idNumbers <- rep(1:10,each = 3) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-23
      • 2015-06-26
      • 1970-01-01
      相关资源
      最近更新 更多