简体   繁体   English

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

[英]data.table shift right all cell values by number of na within each row [R]

How do I shift the cells in a data table TO THE RIGHT by the number of NA in each row in R? 如何将数据表中的单元格向右移动R中每行的NA数量?

Example Data: 示例数据:

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>

Desired Data from example: 示例中的所需数据:

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

Here's one attempt using matrix indexing and a counter of NA values by row: 这是使用矩阵索引和按行对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)

This option is pretty quick and from a brief test churns through 4 columns / 2 million rows in about 3-4 seconds. 此选项非常快捷,只需短暂的测试,即可在3-4秒内遍历4列/ 2百万行。

We can use 我们可以用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM