根据单列值合并多行

时间:2018-10-08 15:30:11

标签: r dataframe dplyr summarization

我有下表:

.center-snackbar {
  text-align: center !important;
}

我希望输出表为:

A  B  C   D    E

1  NA we  are  here
1  hi we  NA   here
1  NA NA  are  there
2  u  NA  are  where

我尝试了以下方法:

A  B   C   D   E
1  hi  we  are here
2  u   NA  are where

它给出了错误:

  

summarise_impl(.data,点)中的错误:列my_fun <- function(x) x[!is.na(x)] buildingCopy %>% group_by(A) %>% summarise_all(funs(my_fun)) 的长度必须为1   (摘要值),而不是3

谁能帮助我实现所需的数据框架。

2 个答案:

答案 0 :(得分:1)

您可以通过以下方式修改功能:

NA

首先,它检查是否有任何非缺失值,然后返回第一个非缺失值,否则返回buildingCopy %>% group_by(A) %>% summarise_all(funs(if_else(any(!is.na(.)), na.exclude(.)[1], NA_character_))) # A tibble: 2 x 5 # A B C D E # <dbl> <chr> <chr> <chr> <chr> # 1 1 hi we are here # 2 2 u NA are where

如果只使用一次功能,您也可以这样做:

summarise_if

或者您可以在buildingCopy %>% add_row(A = 2, B = "u", C = NA_character_, D = "are", E = "where") %>% group_by(A) %>% summarise_if(funs(any(!is.na(.))), funs(na.exclude(.)[1])) # A tibble: 2 x 5 # A B C D E # <dbl> <chr> <chr> <chr> <chr> # 1 1 hi we are here # 2 2 u NA are where 语句中使用该条件:

buildingCopy <- structure(list(A = c(1L, 1L, 1L, 2L), 
                               B = c(NA, "hi", NA, "u"), 
                               C = c("we", "we", NA, NA), 
                               D = c("are", NA, "are", "are"), 
                               E = c("here", "here", "there", "where")), 
                          class = "data.frame", row.names = c(NA, -4L))

数据

public void Foo()

答案 1 :(得分:1)

基本R函数na.omit()可在此处使用

library(dplyr)
my_fun <- function(x) na.omit(x) %>% first()
buildingCopy %>% 
  group_by(A) %>% 
  summarise_all(funs(my_fun))
# A tibble: 2 x 5
      A B     C     D     E    
  <int> <chr> <chr> <chr> <chr>
1     1 hi    we    are   here 
2     2 u     NA    are   where

数据

buildingCopy <- readr::read_table(
"A  B  C   D    E
1  NA we  are  here
1  hi we  NA   here
1  NA NA  are  there
2  u  NA  are  where")