将NA的多列转换为行

时间:2017-08-22 12:22:35

标签: r merge data-cleaning

我有一个大数据框(约100列):

col1| col2| col3| col4| col5| col6| col7
-----------------------------------------
 A     1     2     NA    NA    NA    NA     
 A     3     4     NA    NA    NA    NA    
 B     NA    NA     1     3    NA    NA     
 B     NA    NA     2     3    NA    NA
 C     NA    NA    NA    NA    2     5
 C     NA    NA    NA    NA    3     2

我想将其转换为以下数据框:

col1| merged_col1|
--------------------
 A       1
 A       2
 A       3 
 A       4
 B       1
 B       3
 B       2
 B       3
 C       2
 C       5
 C       3
 C       2

我想将省略所有NA的列组合到相应的行。然后,我想将除第一个之外的所有内容转换为行。我怎么能在R?

2 个答案:

答案 0 :(得分:2)

library(tidyverse)

df <- data.frame(col1 = c("A", "A", "B", "B", "C", "C"),
           col2 = c(1, 3, NA, NA, NA, NA),
           col3 = c(2, 4, NA, NA, NA, NA),
           col4 = c(NA, NA, 1, 2, NA, NA),
           col5 = c(NA, NA, 3, 3, NA, NA),
           col6 = c(NA, NA, NA, NA, 2, 3),
           col7 = c(NA, NA, NA, NA, 5, 2))

df_long <- df %>% gather(col, merged_col1, c(2:7), na.rm = T) %>% select(-col) %>% arrange(col1, merged_col1)

答案 1 :(得分:-1)

For循环在R中使用并不是最好的东西,但你可以尝试这样的东西:

new.df <- data.frame()

index <- 1

for(i in 1:nrow(old.df)){
  for(j in 2:ncol(old.df)){
    if (!is.na(old.df[i,j])){
      new.df[index,1] <- old.df[i,1]
      new.df[index,2] <- old.df[i,j]
      index <- index + 1
    }
  }
}