【问题标题】:Change long variable names in data.frame更改 data.frame 中的长变量名称
【发布时间】:2017-10-19 00:04:58
【问题描述】:

我得到了下面的data.frame df,它有很长的变量名。

每个名称的第一部分是主要类别(岩石、土壤、土地利用),通常由多个名称组成的第二部分是等级(例如对于岩石,两个等级是sandstone mudstone basalt chert limestonesandstone conglomerate coquina tephra )。

> df
# A tibble: 5 x 2
  `rock_sandstone conglomerate coquina tephra` `rock_sandstone mudstone basalt chert limestone`
                                         <dbl>                                            <dbl>
1                                     0.000000                                        18.774037
2                                    41.968310                                        30.276509
3                                    32.804031                                         0.000000
4                                     8.669436                                         3.092062
5                                    32.937377                                        19.894776

我想通过使用每个单词的第一个字母来缩短变量名称,如下所示。 我可以使用例如dplyr::rename 来做到这一点。但是,我有 97 个变量,我想对 20 个具有不同变量名的 data.frames 执行相同的操作。我想知道是否有更快的方法。

library(dplyr)
df <- df %>% rename("r_sccat"  = 'rock_sandstone conglomerate coquina tephra',
                    "r_smbcl" =  "rock_sandstone mudstone basalt chert limestone")

> df
# A tibble: 5 x 2
    r_sccat   r_smbcl
      <dbl>     <dbl>
1  0.000000 18.774037
2 41.968310 30.276509
3 32.804031  0.000000
4  8.669436  3.092062
5 32.937377 19.894776

数据

> dput(df)
structure(list(`rock_sandstone conglomerate coquina tephra` = c(0, 
41.9683095321332, 32.8040311360418, 8.66943642122745, 32.9373770476129
), `rock_sandstone mudstone basalt chert limestone` = c(18.7740373237074, 
30.2765089609693, 0, 3.09206176664796, 19.8947759845006)), row.names = c(NA, 
-5L), class = c("tbl_df", "tbl", "data.frame"), .Names = c("rock_sandstone conglomerate coquina tephra", 
"rock_sandstone mudstone basalt chert limestone"))

【问题讨论】:

    标签: r


    【解决方案1】:

    有点难看,但abbreviate 和一些正则表达式替换会让你到达那里:

    names(df) <- sub("^(.)", "\\1_", abbreviate(gsub("_", " ", names(df))))
    df
    
    ## A tibble: 5 × 2
    #     r_scct   r_smbcl
    #      <dbl>     <dbl>
    #1  0.000000 18.774037
    #2 41.968310 30.276509
    #3 32.804031  0.000000
    #4  8.669436  3.092062
    #5 32.937377 19.894776
    

    【讨论】:

      【解决方案2】:

      我不熟悉 abbreviate,但可以直接通过一些 regexp 替换来实现:

      names( df ) <- gsub( ' ', "", gsub( "([a-z])([a-z]+)", "\\1", names( df ) ) )
      

      使用 magrittr 可以实现更简洁的语法:

      require( magrittr )
      names( df ) %<>%
          gsub( "([a-z])([a-z]+)", "\\1", . ) %>%
          gsub( " ", "", . )
      

      【讨论】:

      • 这实际上可能比我的abbreviate 答案更干净,更适合。
      • 谢谢!我想这取决于你对 R 的内置辅助函数的喜爱程度……我通常觉得它们违反直觉,所以我更喜欢尽可能使用原语。
      • @gorgonzola 感谢您的时间和帮助
      • @gorgonzola 我将不胜感激任何建议对因子级别而不是列名执行相同操作。
      • 您应该能够使用级别( fctor )而不是名称( df )来做同样的事情;名称和级别都返回 R 中的可分配引用,因此相同的过程应该适用于两者。非常抱歉没有早点回复您的评论。
      【解决方案3】:

      这可以通过结合使用 Hadley Wickham 的 stringr 包中的文本清理功能以及 Ingo Feinerer 和 Kurt Hornik 的 tm 包中的文本挖掘功能来完成。我使用 dplyr 作为方便的管道运算符。

      假设您有一个包含长列名称的字符向量的数据集:

      lbl.lst$LongNames

      library(tm)
      library(dplyr)
      library(stringr)
      
      # 1. Create a list of filler words you want removed. 
      # You can add to the list provided by tm using the following code. 
      
      all_stopwrds<- c("words","you","want","removed",stopwords("en"))
      
      # 2. Create a corpus (dataset with your text labels). 
      # Use the tm::tm_map functions to remove filler words, shorten words to their stems, 
      # remove hyphens, numbers, and extra spaces).
      
      z <- Corpus(VectorSource(lbl.lst$LongNames)) %>%
           tm_map(removeWords, all_stopwrds) %>%
           tm_map(stemDocument) %>%
           tm_map(removePunctuation) %>%
           tm_map(removeNumbers) %>%
           tm_map(stripWhitespace) 
      
      
      # 3. Flatten out the corpus to capitalize the first word in each string. Then
      # Choose the first few words (three generally works for me) and 
      # concatenate to get your new variables into CamelCase.  
      
      z <- as.data.frame(do.call(rbind, lapply(z, as.data.frame)))  
      z[,1] <- str_to_title(z[,1]) 
      z$first3 <- word(z[,1], 1,3, sep=" ")
      z$ShortName <- str_replace_all(z$first3, " ", "")
      
      

      【讨论】:

        猜你喜欢
        • 2019-12-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-10
        • 1970-01-01
        相关资源
        最近更新 更多