【问题标题】:pasting values based on column names根据列名粘贴值
【发布时间】:2017-07-29 13:50:23
【问题描述】:

我在我类似的question 上问一个与@Axeman 正确答案有关的问题。下面是我的dataframeoutputdput

我正在尝试从 2 组变量(相同长度 N)中执行 paste 字符串。

var1.x, var2.x, var1.y, var2.y 的字符串应该变成 var1, var2 。我正在寻找可以支持varN.x, varN.y变成varN的代码

我已经调整了@Axeman 的答案,但是在粘贴 N 个变量时它不会缩放。

df[,3:4] <- mapply(function(x, y) paste0(na.omit(c(x, y)), collapse = ''), 
                    as.character(df[,3:4]), as.character(df[,5:6]))
output <- df[1:4]

df <- structure(list(factor1 = structure(c(1L, 1L, 2L, 1L, 1L, 2L, 
2L, 1L), .Label = c("f1", "f2"), class = "factor"), factor2 = c(1L, 
2L, 1L, 3L, 4L, 2L, 3L, 5L), var1.x = structure(c(1L, 2L, NA, 
3L, 4L, 6L, 7L, 5L), .Label = c("a", "d", "g", "h", "j", "t", 
"y"), class = "factor"), var2.x = structure(c(NA, 1L, 2L, NA, 
1L, 2L, 2L, 2L), .Label = c("g", "s"), class = "factor"), var1.y = structure(c(4L, 
1L, NA, 2L, 2L, 2L, NA, 3L), .Label = c("f", "g", "h", "x"), class = "factor"), 
    var2.y = structure(c(4L, 2L, 2L, 1L, NA, 3L, 3L, 3L), .Label = c("a", 
    "g", "h", "t"), class = "factor")), .Names = c("factor1", 
"factor2", "var1.x", "var2.x", "var1.y", "var2.y"), class = "data.frame", row.names = c(NA, 
-8L))

output <- structure(list(factor1 = structure(c(1L, 1L, 2L, 1L, 1L, 2L, 
2L, 1L), .Label = c("f1", "f2"), class = "factor"), factor2 = c(1L, 
2L, 1L, 3L, 4L, 2L, 3L, 5L), var1 = structure(c(1L, 2L, NA, 3L, 
4L, 6L, 7L, 5L), .Label = c("ax", "df", "gg", "hg", "js", "tg", 
"y"), class = "factor"), var2 = structure(c(7L, 3L, 5L, 1L, 2L, 
6L, 6L, 4L), .Label = c("a", "g", "gg", "hh", "sg", "sh", "t"
), class = "factor")), .Names = c("factor1", "factor2", "var1", 
"var2"), class = "data.frame", row.names = c(NA, -8L))

【问题讨论】:

    标签: r string paste


    【解决方案1】:

    一种通过基础 R 实现它的方法,

    #make sure the columns you are pasting are characters
    df[-c(1:2)] <- lapply(df[-c(1:2)], as.character)
    
    #replace NA with '' to avoid pasting problems
    df[is.na(df)] <- ''
    
    #create a vector with unique column names 
    ind <- unique(sub('\\..*', '', names(df[-c(1:2)])))
    
    #create a matrix matching each column name with ind, in order to use as index
    m1 <- t(sapply(ind, grepl, names(df[-c(1:2)])))
    
    #apply paste0 in columns based on index matrix m1.
    df1 <- setNames(data.frame(sapply(seq(nrow(m1)), function(i)
                    do.call(paste0, df[-c(1:2)][m1[i,]]))), paste0('Var', seq(nrow(m1))))
    
    #bind it back to first two columns of original df and change '' to NA
    df <- cbind(df[1:2], df1)
    df[df == ''] <- NA
    
    df
    #  factor1 factor2 Var1 Var2
    #1      f1       1   ax    t
    #2      f1       2   df   gg
    #3      f2       1 <NA>   sg
    #4      f1       3   gg    a
    #5      f1       4   hg    g
    #6      f2       2   tg   sh
    #7      f2       3    y   sh
    #8      f1       5   jh   sh
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-28
      • 2017-09-21
      • 2021-11-26
      • 1970-01-01
      • 2013-11-27
      • 2013-12-11
      • 1970-01-01
      相关资源
      最近更新 更多