拆分具有不同数值的列

时间:2016-09-18 13:12:34

标签: r multiple-columns tidyr

我想将1列拆分为3列。问题是,如果我用分隔符“,”拆分列,它有时会包含3个值,有时会包含2个值。

输出应如下所示:

  rooms                          location           Street   Suburb    City  price m2
1     2           examplestreet 3, munich             <NA>     <NA>  munich 500000 40
2     3                   suburg2, berlin             <NA>  suburg2  berlin 300000 60
3     5 examplestreet 45, suburb1, munich examplestreet 45  suburb1  munich 350000 55

这是我做的:

   > rooms = c(2, 3, 5) 
> location = c("examplestraße 3, munich", "suburb2, berlin", "examplestaße 45, suburb1, munich") 
> price = c(500000, 300000, 350000) 
> m2 = c(40, 60, 55) 
> 
> dataexample = data.frame(rooms, location, price, m2)
> dataexample
  rooms                         location  price m2
1     2          examplestraße 3, munich 500000 40
2     3                  suburb2, berlin 300000 60
3     5 examplestaße 45, suburb1, munich 350000 55
> rooms = c(2, 3, 5) 
> location = c("munich", "suburb2, berlin", "examplestaße 45, suburb1, munich") 
> price = c(500000, 300000, 350000) 
> m2 = c(40, 60, 55) 
> 
> dataexample = data.frame(rooms, location, price, m2)
> 
> library(tidyr)
> separate(dataexample, location, into=c('Street', 'Suburb', "City"), remove=FALSE, sep = ",")
  rooms                         location          Street   Suburb    City  price m2
1     2                           munich          munich     <NA>    <NA> 500000 40
2     3                  suburb2, berlin         suburb2   berlin    <NA> 300000 60
3     5 examplestaße 45, suburb1, munich examplestaße 45  suburb1  munich 350000 55
Warning message:
Too few values at 2 locations: 1, 2 

1 个答案:

答案 0 :(得分:0)

我们也可以通过分割“&#39;位置”来base R执行此操作。分隔符(,)的列,填充了具有少量元素的list元素的NA,rbind list元素和cbind与原始数据集

lst <- strsplit(as.character(dataexample$location), ", ")
cbind(dataexample, `colnames<-`(do.call(rbind, lapply(lapply(lst, 
      `length<-`, max(lengths(lst))), 
        function(x) c(x[is.na(x)], x[!is.na(x)]))), c('Street', 'Suburb', "City")))
#  rooms                         location  price m2          Street  Suburb   City
#1     2                           munich 500000 40            <NA>    <NA> munich
#2     3                  suburb2, berlin 300000 60            <NA> suburb2 berlin
#3     5 examplestaße 45, suburb1, munich 350000 55 examplestaße 45 suburb1 munich