【问题标题】:Split dataframe on filtered character and make multiple new columns在过滤字符上拆分数据框并创建多个新列
【发布时间】:2015-05-11 07:51:45
【问题描述】:

我有一个在我的工作中很常见的数据预处理问题。我通常有两个文件,我最终想要对其进行大型匹配操作。 它通常是一个两步过程,其中第一步涉及制作第一个文件的“清理”数据帧,第二步是与更大数据帧的第二个文件进行匹配(vlookup)。我在这个问题的第一步需要帮助。 我在下面创建了一个简单的示例来处理。 我的简化数据框:

c1 <- 1:15
c2 <- c("Valuelabels", "V1", "1", "2", "Valuelabels", "V2", "1", "2", "3", "Valuelabels", "V3", "1", "2", "3", "4")
c3 <- c("", "", "Male", "Female", "", "", "Married", "Single", "Other", "", "", "SingleWithChildren", "SingleWithoutChildren","MarriedWithChildren", "PartneredWithChildren") 

df <- data.frame(row.names =c1,c2,c3)
df

            c2                    c3
1  Valuelabels                      
2           V1                      
3            1                  Male
4            2                Female
5  Valuelabels                      
6           V2                     
7            1               Married
8            2                Single
9            3                 Other
10 Valuelabels                      
11          V3                      
12           1    SingleWithChildren
13           2 SingleWithoutChildren
14           3   MarriedWithChildren
15           4 PartneredWithChildren

现在,我想在第一列的“Valuelabel”字符串上拆分数据框,最终得到一个如下所示的新数据框:

   V1 V1_match V2 V2_match V3              V3_match
1:  1     Male  1  Married  1    SingleWithChildren
2:  2   Female  2   Single  2 SingleWithoutChildren
3: NA           3    Other  3   MarriedWithChildren
4: NA          NA           4 PartneredWithChildren

我最后想创建一个数据框,其中 V1 作为列名,在这些列名下的匹配值作为一个新列,在我的示例 V1_match... 中命名,依此类推,用于 V2 到 V3。

此数据框将在将其与更大的数据框匹配之前结束我的第一步。

非常感谢帮助。

【问题讨论】:

    标签: r dataframe reshape


    【解决方案1】:

    这是一个可能的data.table 解决方案

    library(data.table) # v 1.9.5
    setDT(df)[, indx := c2[2L], by = cumsum(c2 == "Valuelabels")]
    df2 <- df[!grepl("\\D", c2)][, indx2 := seq_len(.N), by = indx]
    dcast(df2, indx2 ~ indx, value.var = c("c2", "c3"))
    #    indx2 V1_c2 V2_c2 V3_c2  V1_c3   V2_c3                 V3_c3
    # 1:     1     1     1     1   Male Married    SingleWithChildren
    # 2:     2     2     2     2 Female  Single SingleWithoutChildren
    # 3:     3    NA     3     3     NA   Other   MarriedWithChildren
    # 4:     4    NA    NA     4     NA      NA PartneredWithChildren
    

    您需要安装data.table v > 1.9.5 才能使用

    library(devtools)
    install_github("Rdatatable/data.table", build_vignettes = FALSE)
    

    【讨论】:

    • 我有点不清楚,但我也想在我的示例中保留 (V1,V2,V3) 的列,有时在我的数据中它们是字符串,有时它们是数字,但所有有独特的意义
    • 好的,看看我的编辑。我希望它有所帮助。之后您可以重新排序该列。
    【解决方案2】:

    另一种方法基R

    lst = lapply(split(df,cumsum(df$c2=='Valuelabels')), tail, -2)
    Reduce(function(u,v) merge(u,v,by='c2',all=T), lst)
    #  c2   c3.x    c3.y                    c3
    #1  1   Male Married    SingleWithChildren
    #2  2 Female  Single SingleWithoutChildren
    #3  3   <NA>   Other   MarriedWithChildren
    #4  4   <NA>    <NA> PartneredWithChildren
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-07
      • 1970-01-01
      相关资源
      最近更新 更多