Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何将键/值字符串转换为单独的列?_R_Split_Multiple Columns_Key Value - Fatal编程技术网

R 如何将键/值字符串转换为单独的列?

R 如何将键/值字符串转换为单独的列?,r,split,multiple-columns,key-value,R,Split,Multiple Columns,Key Value,我有一个data.frame和key/value string列,其中包含一组用户的特征及其值的信息。大概是这样的: data<-data.frame(id=1:3,statid=c("s003e","s093u","s085t"),str=c("a:1,7:2","a:1,c:4","a:3,b:5,c:33")) data # id statid str # 1 1 s003e a:1,7:2 # 2 2 s093u a:1,c:4 #

我有一个
data.frame
key/value string
列,其中包含一组用户的特征及其值的信息。大概是这样的:

data<-data.frame(id=1:3,statid=c("s003e","s093u","s085t"),str=c("a:1,7:2","a:1,c:4","a:3,b:5,c:33"))
data
#   id statid          str
# 1  1  s003e      a:1,7:2
# 2  2  s093u      a:1,c:4
# 3  3  s085t a:3,b:5,c:33

data您可以使用
dplyr
tidyr

library(dplyr); library(tidyr)
data %>% mutate(str = strsplit(str, ",")) %>% unnest(str) %>% 
         separate(str, into = c('var', 'val'), sep = ":") %>% spread(var, val, fill = 0)

#   id statid 7 a b  c
# 1  1  s003e 2 1 0  0
# 2  2  s093u 0 1 0  4
# 3  3  s085t 0 3 5 33

我们可以使用
cSplit
以更干净的方式完成这项工作。通过在
处拆分,将数据转换为“长”格式,然后在
dcast
处将数据从“长”拆分为“宽”

library(splitstackshape)
library(data.table)
dcast(cSplit(cSplit(data, "str", ",", "long"), "str", ":"), 
                    id+statid~str_1, value.var="str_2", fill = 0)
#   id statid 7 a b  c
#1:  1  s003e 2 1 0  0
#2:  2  s093u 0 1 0  4
#3:  3  s085t 0 3 5 33

请参见
splitstackshape
package。
library(splitstackshape)
library(data.table)
dcast(cSplit(cSplit(data, "str", ",", "long"), "str", ":"), 
                    id+statid~str_1, value.var="str_2", fill = 0)
#   id statid 7 a b  c
#1:  1  s003e 2 1 0  0
#2:  2  s093u 0 1 0  4
#3:  3  s085t 0 3 5 33