簡體   English   中英

通過刪除R中的NA來組合2個以上的列

[英]Combining more than 2 columns by removing NA's in R

乍一看,這似乎是Combine / merge列的重復, 同時避免了NA? 但事實上並非如此。 我有時會處理兩列以上而不是兩列。

我的數據框看起來像這樣:

     col1 col2 col3 col4 col5
[1,]    1   NA   NA   13   NA
[2,]   NA   NA   10   NA   18
[3,]   NA    7   NA   15   NA
[4,]    4   NA   NA   16   NA

現在,我希望將此數據框“折疊”為具有較少列和刪除NA的數據幀。 事實上,我正在尋找和“卓越的做法”:刪除一個單元格,整行將向左移動一個單元格。

此示例中的結果將是:

     col1 col2 
[1,]    1   13   
[2,]   10   18   
[3,]    7   15   
[4,]    4   16   

有沒有人知道如何在R中這樣做? 提前謝謝了!

您可以使用apply 如果df是你的數據幀`:

df2 <- apply(df,1,function(x) x[!is.na(x)])
df3 <- data.frame(t(df2))
colnames(df3) <- colnames(df)[1:ncol(df3)]

輸出:

#      col1 col2
#         1   13
#        10   18
#         7   15
#         4   16

您可以使用applyna.exclude

DF
##   V1 V2 V3 V4 V5
## 1  1 NA NA 13 NA
## 2 NA NA 10 NA 18
## 3 NA  7 NA 15 NA
## 4  4 NA NA 16 NA

t(apply(DF, 1, na.exclude))
##      [,1] [,2]
## [1,]    1   13
## [2,]   10   18
## [3,]    7   15
## [4,]    4   16

如果要保持data.frame的維度相同,可以使用sortna.last=TRUE 這也將處理您在不同行中具有不等數量值的情況。

t(apply(DF, 1, sort, na.last = T))
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1   13   NA   NA   NA
## [2,]   10   18   NA   NA   NA
## [3,]    7   15   NA   NA   NA
## [4,]    4   16   NA   NA   NA

這個功能有點啰嗦,但(1)從長遠來看會更快,(2)它提供了很大的靈活性:

myFun <- function(inmat, outList = TRUE, fill = NA, origDim = FALSE) {
  ## Split up the data by row and isolate the non-NA values
  myList <- lapply(sequence(nrow(inmat)), function(x) {
    y <- inmat[x, ]
    y[!is.na(y)]
  })
  ## If a `list` is all that you want, the function stops here
  if (isTRUE(outList)) {
    myList
  } else {
    ## If you want a matrix instead, it goes on like this
    Len <- vapply(myList, length, 1L)
    ## The new matrix can be either just the number of columns required
    ##   or it can have the same number of columns as the input matrix
    if (isTRUE(origDim)) Ncol <- ncol(inmat) else Ncol <- max(Len)
    Nrow <- nrow(inmat)
    M <- matrix(fill, ncol = Ncol, nrow = Nrow)
    M[cbind(rep(sequence(Nrow), Len), sequence(Len))] <- 
      unlist(myList, use.names=FALSE)
    M
  }
}

為了測試它,讓我們創建一個函數來組成一些虛擬數據:

makeData <- function(nrow = 10, ncol = 5, pctNA = .8, maxval = 25) {
  a <- nrow * ncol
  m <- matrix(sample(maxval, a, TRUE), ncol = ncol)
  m[sample(a, a * pctNA)] <- NA
  m
}

set.seed(1)
m <- makeData(nrow = 5, ncol = 4, pctNA=.6)
m
#      [,1] [,2] [,3] [,4]
# [1,]   NA   NA   NA   NA
# [2,]   10   24   NA   18
# [3,]   NA   17   NA   25
# [4,]   NA   16   10   NA
# [5,]   NA    2   NA   NA

......並應用它......

myFun(m)
# [[1]]
# integer(0)
# 
# [[2]]
# [1] 10 24 18
# 
# [[3]]
# [1] 17 25
# 
# [[4]]
# [1] 16 10
# 
# [[5]]
# [1] 2

myFun(m, outList = FALSE)
#      [,1] [,2] [,3]
# [1,]   NA   NA   NA
# [2,]   10   24   18
# [3,]   17   25   NA
# [4,]   16   10   NA
# [5,]    2   NA   NA

## Try also
## myFun(m, outList = FALSE, origDim = TRUE)

而且,與目前為止的其他答案相比,讓我們對更大的數據運行一些時間:

set.seed(1)
m <- makeData(nrow = 1e5, ncol = 5, pctNA = .75)

## Will return a matrix
funCP <- function(inmat) t(apply(inmat, 1, sort, na.last = T))
system.time(funCP(m))
#    user  system elapsed 
#   9.776   0.000   9.757 

## Will return a list in this case
funJT <- function(inmat) apply(inmat, 1, function(x) x[!is.na(x)])
system.time(JT <- funJT(m))
#    user  system elapsed 
#   0.577   0.000   0.575 

## Output a list
system.time(AM <- myFun(m))
#    user  system elapsed 
#   0.469   0.000   0.466 

identical(JT, AM)
# [1] TRUE

## Output a matrix
system.time(myFun(m, outList=FALSE, origDim=TRUE))
#    user  system elapsed 
#   0.610   0.000   0.612 

因此, list輸出看起來比@ JT85的解決方案略快,並且matrix輸出看起來稍慢。 但是,與逐行sort相比,這是一個明顯的改進。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM