使用R中的一个数据框合并

时间:2018-06-19 00:21:48

标签: r dataframe indexing merge

我在R中有一个数据框,第一列中存储了重复的索引。

df <- data.frame("Index" = c(1,2,1), "Age" = c("Jane Doe","John Doe","Jane 
Doe"), "Address" = c("123 Fake Street","780 York Street","456 Elm 
Street"),"Telephone" = c("xxx-xxx-xxxx","zzz-zzz-zzzz","yyy-yyy-yyyy"))

Index    Name        Address          Telephone
  1    Jane Doe  123 Fake Street   xxx-xxx-xxxx
  2    John Doe  780 York Street   zzz-zzz-zzzz
  1    Jane Doe  456 Elm Street    yyy-yyy-yyyy

我想将上面的数据框结合起来:

Index   Name        Address         Telephone     Address 2       Telephone 2
1    Jane, Doe  123 Fake Street   xxx-xxx-xxxx  456 Elm Street  yyy-yyy-yyyy
2    John Doe   780 York Street   zzz-zzz-zzzz       NA             NA

我可以使用&#34;合并&#34;在相同的数据框架上或者是R中的另一个命令可以完成此任务吗?谢谢。

2 个答案:

答案 0 :(得分:4)

tidyverse

df %>%
  group_by(Age) %>%
  summarize_at(vars(Telephone,Address),paste, collapse="|") %>%
  separate(Address,into=c("Address1","Address2"),sep="\\|") %>%
  separate(Telephone,into=c("Telephone1","Telephone2"),sep="\\|")

# # A tibble: 2 x 5
#   Age      Telephone1   Telephone2   Address1        Address2      
#   <fct>    <chr>        <chr>        <chr>           <chr>         
# 1 Jane Doe xxx-xxx-xxxx yyy-yyy-yyyy 123 Fake Street 456 Elm Street
# 2 John Doe zzz-zzz-zzzz <NA>         780 York Street <NA> 

更一般地说,我们可以使用summarizelist嵌套值,并使用正确的格式将内容重新格式化为unnest

df %>%
  group_by(Age) %>%
  summarize_at(vars(Telephone,Address),
               ~lst(setNames(invoke(tibble,.),seq_along(.)))) %>%
  unnest(.sep = "")

# # A tibble: 2 x 5
#   Age      Telephone1   Telephone2   Address1        Address2      
#   <fct>    <fct>        <fct>        <fct>           <fct>         
# 1 Jane Doe xxx-xxx-xxxx yyy-yyy-yyyy 123 Fake Street 456 Elm Street
# 2 John Doe zzz-zzz-zzzz <NA>         780 York Street <NA> 

汇总内部的函数有点可怕,但是如果你想再次使用它,你可以把它包装成一个更友好的名字(我添加了一个名字参数以防万一):

nest2row <- function(x,names = seq_along(x))
  lst(setNames(invoke(tibble,x),names[seq_along(x)])) 

df %>%
  group_by(Age) %>%
  summarize_at(vars(Telephone,Address), nest2row) %>%
  unnest(.sep = "")

我认为这是推荐的整洁方式:

df %>%
  group_by(Age) %>%
  mutate(id=row_number()) %>%
  gather(key,value,Address,Telephone) %>%
  unite(key,key,id,sep="") %>%
  spread(key,value)

# # A tibble: 2 x 6
# # Groups:   Age [2]
#   Index Age      Address1        Address2       Telephone1   Telephone2  
#   <dbl> <fct>    <chr>           <chr>          <chr>        <chr>       
# 1     1 Jane Doe 123 Fake Street 456 Elm Street xxx-xxx-xxxx yyy-yyy-yyyy
# 2     2 John Doe 780 York Street <NA>           zzz-zzz-zzzz <NA>

使用我的第二个解决方案,您可以保留您的因素,并且不会在惯用方式的同一列中强制使用不同类型的变量。

答案 1 :(得分:0)

尝试这样的事情:

df <- data.frame("Index" = c(1,2,1), "Age" = c("Jane Doe","John Doe","Jane Doe"), 
"Address" = c("123 Fake Street","780 York Street","456 Elm Street"),
"Telephone" = c("xxx-xxx-xxxx","zzz-zzz-zzzz","yyy-yyy-yyyy"),
                 stringsAsFactors = F)

df$unindex=paste(df$Index,df$Age)

sapply(unique(df$unindex),function(li){ # li="1 Jane Doe"
  dft=df[li==df$unindex,3:4]
  if(nrow(dft)==1)dft else c(t(dft))
})