data.table在每行[R]中按na的数量向右移动所有单元格值

时间:2018-11-29 03:51:18

标签: r dataframe data.table shift

如何将数据表中的单元格向右移动R中每行的NA数量?

示例数据:

data <- data.table(c1=c("a","e","h","j"),
                   c2=c("b","f","i",NA),
                   c3=c("c","g",NA,NA),
                   c4=c("d",NA,NA,NA), stringsAsFactors = F)
  c1   c2   c3   c4
1  a    b    c    d
2  e    f    g <NA>
3  h    i <NA> <NA>
4  j <NA> <NA> <NA>

示例中的所需数据:

data.desired <- data.table(
                  c1=c("a",NA,NA,NA),
                   c2=c("b","e",NA,NA),
                   c3=c("c","f","h",NA),
                   c4=c("d","g","i","j"), stringsAsFactors = F)
    c1   c2   c3 c4
1    a    b    c  d
2 <NA>    e    f  g
3 <NA> <NA>    h  i
4 <NA> <NA> <NA>  j

2 个答案:

答案 0 :(得分:3)

这是尝试使用矩阵索引和按行计算NA个值的计数器:

#convert back to a data.frame to take advantage of matrix indexing
setDF(data)   

arr <- which(!is.na(data), arr.ind=TRUE)
arr[,"col"] <- arr[,"col"] + rowSums(is.na(data))[arr[,"row"]]
out <- data
out[] <- NA
out[arr] <- data[!is.na(data)]

out
#    c1   c2   c3 c4
#1    a    b    c  d
#2 <NA>    e    f  g
#3 <NA> <NA>    h  i
#4 <NA> <NA> <NA>  j

#convert to data.table if necessary
setDT(out)

此选项非常快捷,只需短暂的测试就可以在3-4秒内遍历4列/ 200万行。

答案 1 :(得分:1)

我们可以使用

data.table(t(apply(data, 1, function(x){ c(rep(NA, sum(is.na(x))), x[!is.na(x)])})))
#      V1   V2   V3 V4
# 1:    a    b    c  d
# 2: <NA>    e    f  g
# 3: <NA> <NA>    h  i
# 4: <NA> <NA> <NA>  j