R编程连接每3行并删除NA

时间:2017-03-18 09:15:38

标签: r dataframe concatenation na

我有这个数据框,日期列分为多行。我想连接或合并日期行并从其他列中删除NA

   df <- data.frame(date = rep(c("Nov","22","2012"),3),x = rep(c(NA,NA,12345),3))
        date    x
      1  Nov    NA
      2   22    NA
      3 2012 12345
      4  Nov    NA
      5   22    NA
      6 2012 12345
      7  Nov    NA
      8   22    NA
      9 2012 12345

期望的结果

     df <- data.frame(date = rep(("Nov 22 2012"),3),x  = rep(12345,3))

           date    x
  1 Nov 22 2012 12345
  2 Nov 22 2012 12345
  3 Nov 22 2012 12345

谢谢

3 个答案:

答案 0 :(得分:0)

一种方法是使用zoo包,

library(zoo)

new_df <- as.data.frame(rollapply(df, 3, by = 3, function(i)na.omit(c(i))))
final_df <- cbind(date = do.call(paste, new_df[1:3]), new_df[-c(1:3)])

final_df
#         date    V4
#1 Nov 22 2012 12345
#2 Nov 22 2012 12345
#3 Nov 22 2012 12345

答案 1 :(得分:0)

如果您的data.frame结构与您在示例中显示的结构完全相同,则可能有所帮助:

df <- do.call(rbind, 
              lapply(seq(1, nrow(df), by = 3),
                     function(i) 
                             data.frame(date = paste(df[i:(i+2),1], 
                                                     collapse = "-"),
                                        x = na.omit(df[i:(i+2), 2])
                                        )
                     )
              )
#df
#         date     x
#1 Nov-22-2012 12345
#2 Nov-22-2012 12345
#3 Nov-22-2012 12345

答案 2 :(得分:0)

您可以使用dplyr中的lag函数将日期粘贴在一起,然后删除x列中的NA ...

library(dplyr)
df <- df %>% mutate(date=paste(lag(date,2),lag(date),date)) %>% filter(!is.na(x))