【问题标题】:Dynamically make data frame value column name动态制作数据框值列名
【发布时间】:2018-03-08 20:08:20
【问题描述】:

我正在尝试获取列中的值以设置为列名。冒号前的字符应该是列名。

df = cbind.data.frame(
    id = c(1, 2 ,3, 4, 5),
    characteristics_ch1 = c("gender: Female", "gender: Male", "gender: Female", "gender: Male", "gender: Female"),
    characteristics_ch1.1 = c("Thing One: a", "Thing One: a", "Thing One: a", "Thing One: b", "Thing One: b"),
    characteristics_ch1.2 = c("age: 60", "age: 45", "age: 63", "age: 56", "age: 65"))

对于第 2-5 列,我想删除“gender:”、“Thing One:”和“age:”,使它们成为各自列的名称。

生成的数据框将是:

Result = cbind.data.frame(
        id = c(1, 2 ,3, 4, 5),
        gender = c("Female", "Male", "Female", "Male", "Female"),
        `Thing One` = c("a", "a", "a", "b", "b"),
        age = c("60", "45", "63", "56", "65")
)

为此,我正在运行以下函数:

re_col = function(i){
        new_name = str_split_fixed(i, ": ", 2)[1]
        return(assign(new_name, str_split_fixed(i, ": ", 2)[,2]))
}

通过以下应用函数:

plyr::colwise(re_col)(df)

#and

purrr::map(df, re_col)

没有成功。

还有更好的方法。我最初尝试编写一个可以在数据清理中与 dplyr 一起使用的函数,作为 %>% 步骤,但没有成功。

【问题讨论】:

    标签: r dplyr purrr


    【解决方案1】:

    我们可以将gather数据框转成长格式,separate值列通过:,然后spread数据框转回宽格式。

    library(tidyverse)
    
    df2 <- df %>%
      gather(Column, Value, -id) %>%
      separate(Value, into = c("New_Column", "Value"), sep = ": ") %>%
      select(-Column) %>%
      spread(New_Column, Value, convert = TRUE)
    df2
    #   id age gender Thing One
    # 1  1  60 Female         a
    # 2  2  45   Male         a
    # 3  3  63 Female         a
    # 4  4  56   Male         b
    # 5  5  65 Female         b
    

    【讨论】:

    • 太棒了!这是一个非常简单的解决方案,希望我能想到!
    【解决方案2】:

    一种解决方法,使用stringi 通过提供给指定列的正则表达式模式拆分数据值

    rename.df_cols <- function(df, rgx_pattern = NULL, col_idx = NULL,...){
        if(max(col_idx) > ncol(df)){
            col_idx <- min(col_idx):ncol(df)
        }
        o <- lapply(col_idx, function(i){
        
            parts <- stri_split_regex(df[[i]], rgx_pattern, simplify = T)
            col_name <- unique(parts[,1])
            new_dat <- parts[,2]
            
            colnames(df)[[i]] <<- col_name
            df[[i]] <<- new_dat
        })
        return(df)
    }
    
    > df
      id characteristics_ch1 characteristics_ch1.1 characteristics_ch1.2
    1  1      gender: Female          Thing One: a               age: 60
    2  2        gender: Male          Thing One: a               age: 45
    3  3      gender: Female          Thing One: a               age: 63
    4  4        gender: Male          Thing One: b               age: 56
    5  5      gender: Female          Thing One: b               age: 65
    > rename.df_cols(df = df, col_idx = 2:4, rgx_pattern = "(\\s+)?\\:(\\s+)?")
      id gender Thing One age
    1  1 Female         a  60
    2  2   Male         a  45
    3  3 Female         a  63
    4  4   Male         b  56
    5  5 Female         b  65
    

    这就是你要找的吗?

    用管道编辑:

    > df %>% rename.df_cols(rgx_pattern = "(\\s+)?\\:(\\s+)?", col_idx = 2:5)
      id gender Thing One age
    1  1 Female         a  60
    2  2   Male         a  45
    3  3 Female         a  63
    4  4   Male         b  56
    5  5 Female         b  65
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-15
      • 1970-01-01
      • 2014-08-18
      • 2021-08-24
      • 2016-06-21
      • 1970-01-01
      相关资源
      最近更新 更多