我已经尝试在SO here和here解决这个问题了 - 原因得到了很好的答案,但意识到这只是我认为是一般问题的部分解决方案:通常数据已经存在组织为有变量(显然最有趣的是)作为每个变量一列,然后是最后一列,其中几个变量值对被放在一起。我一直在努力寻找将最后一列变量转换为单独列的一般方法,这样整理数据不应该是tidyr
的工作吗?
require(dplyr)
require(stringr)
data <-
data.frame(
shoptype=c("A","B","B"),
city=c("bah", "bah", "slah"),
sale=c("type cheese; price 200", "type ham; price 150","type cheese; price 100" )) %>%
tbl_df()
> data
Source: local data frame [3 x 3]
shoptype city sale
1 A bah type cheese; price 200
2 B bah type ham; price 150
3 B slah type cheese; price 100
这里我们有一些关于一些城市的商店的信息,这些商店有一个连接的列,变量用&#34;;&#34;和var-val与空间。 人们希望输出如下:
shoptype city type price
1 A bah cheese 200
2 B bah ham 150
3 B slah cheese 100
当所有行都是唯一的行时(参见链接的SO问题)
require(plyr)
require(dplyr)
require(stringr)
require(tidyr)
data %>%
mutate(sale = str_split(as.character(sale), "; ")) %>%
unnest(sale) %>%
mutate(sale = str_trim(sale)) %>%
separate(sale, into = c("var", "val")) %>%
spread(var, val)
但如果我们将第二排店铺类型更改为&#34; A&#34;因为这个我们得到一个错误。像:
data2 <-
data.frame(
shoptype=c("A","A","B"),
city=c("bah", "bah", "slah"),
sale=c("type cheese; price 200", "type ham; price 150","type cheese; price 100" )) %>%
tbl_df()
data2 %>%
mutate(sale = str_split(as.character(sale), "; ")) %>%
unnest(sale) %>%
mutate(sale = str_trim(sale)) %>%
separate(sale, into = c("var", "val")) %>%
spread(var, val)
Error: Duplicate identifiers for rows (2, 4), (1, 3)
我尝试用唯一的ID来解决这个问题(再次看到链接的SO答案):
data2 %>%
mutate(sale = str_split(as.character(sale), "; ")) %>%
unnest(sale) %>%
mutate(sale = str_trim(sale),
v0=rownames(.)) %>%
separate(sale, into = c("var", "val")) %>%
spread(var, val)
Source: local data frame [6 x 5]
shoptype city v0 price type
1 A bah 1 NA cheese
2 A bah 2 200 NA
3 A bah 3 NA ham
4 A bah 4 150 NA
5 B slah 5 NA cheese
6 B slah 6 100 NA
这给出了结构缺失数据,我无法弄清楚如上所述的上述输出中所描述的如何收集。
我想我真的错过了一些属于tidyr范围的东西(我希望!)。
答案 0 :(得分:6)
我不认为需要使用tidyr::unnest
和tidyr::gather
。这是一个专注于stringr::str_replace
和tidyr::separate
的替代解决方案:
library(dplyr)
library(stringr)
library(tidyr)
data2 %>%
mutate(
sale = str_replace(sale, "type ", ""),
sale = str_replace(sale, " price ", "")
) %>%
separate(sale, into = c("type", "price"), sep = ";")
# Source: local data frame [3 x 4]
# shoptype city type price
# 1 A bah cheese 200
# 2 A bah ham 150
# 3 B slah cheese 100
答案 1 :(得分:4)
在拆分前添加辅助ID:
data2 %>%
group_by(shoptype, city) %>%
mutate(id2 = sequence(n())) %>%
mutate(sale = str_split(as.character(sale), "; ")) %>%
unnest(sale) %>%
mutate(sale = str_trim(sale)) %>%
separate(sale, into = c("var", "val")) %>%
spread(var, val)
# Source: local data frame [3 x 5]
#
# shoptype city id2 price type
# 1 A bah 1 200 cheese
# 2 A bah 2 150 ham
# 3 B slah 1 100 cheese
如果你使用我的“splitstackshape”包中的一些函数,代码可以变得更紧凑:
as.data.frame(data2) %>%
getanID(c("shoptype", "city")) %>%
cSplit("sale", ";", "long") %>%
cSplit("sale", " ") %>%
spread(sale_1, sale_2)
# shoptype city .id price type
# 1: A bah 1 200 cheese
# 2: A bah 2 150 ham
# 3: B slah 1 100 cheese
答案 2 :(得分:4)
上面有两个很好的答案,但认为extract
data2 %>%
extract(sale, c("type", "price"), "type (.+); price (.+)", convert = TRUE)