R:使用矢量作为tidyr扩散函数的关键参数
我试图使用tidyr扩散函数,除了我想传入我自己的特征名称向量以用于关键参数 例如,默认用法是R:使用矢量作为tidyr扩散函数的关键参数,r,data-manipulation,tidyr,spread,R,Data Manipulation,Tidyr,Spread,我试图使用tidyr扩散函数,除了我想传入我自己的特征名称向量以用于关键参数 例如,默认用法是 test<-data.frame(id=c(1,1,2,2), feat=c("feat1", "feat2", "feat1", "feat2"), value = c(10,20, 1000, 2000)) test %>% spread(key = feat, value = value, fill = 0) id feat1
test<-data.frame(id=c(1,1,2,2),
feat=c("feat1", "feat2", "feat1", "feat2"),
value = c(10,20, 1000, 2000))
test %>% spread(key = feat, value = value, fill = 0)
id feat1 feat2
1 1 10 20
2 2 1000 2000
test%排列(key=feat,value=value,fill=0)
id feat1 feat2
1 1 10 20
2 2 1000 2000
我想传入我自己的特征字符串向量,用作键,类似这样的东西
featlist<-c("feat1", "feat2", "feat3")
test %>% spread(key = featlist, value = value, fill = 0)
#desired output
id feat1 feat2 feat3
1 1 10 20 0
2 2 1000 2000 0
#Error output
Error: `var` must evaluate to a single number or a column name, not a character vector
#Trying spread_
test %>% spread_(key = featlist, value = "value", fill = 0)
Error: Only strings can be converted to symbols
featlist%排列(key=featlist,value=value,fill=0)
#期望输出
id feat1 feat2 feat3
1 1 10 20 0
2 2 1000 2000 0
#错误输出
错误:`var`必须计算为单个数字或列名,而不是字符向量
#尝试传播_
测试%>%spread(键=特征列表,值=“值”,填充=0)
错误:只能将字符串转换为符号
只需将专长列设置为一个系数,级别设置为featlist
,然后将drop
参数设置为FALSE
,如下所示:
test<-data.frame(id=c(1,1,2,2),
feat=c("feat1", "feat2", "feat1", "feat2"),
value = c(10,20, 1000, 2000))
featlist<-c("feat1", "feat2", "feat3")
test$feat <- factor(test$feat, levels = featlist)
test %>% spread(key = feat, value = value, fill = 0, drop = FALSE)
只需将专长列设置为一个系数,级别设置为
featlist
,然后将drop
参数设置为FALSE
,如下所示:
test<-data.frame(id=c(1,1,2,2),
feat=c("feat1", "feat2", "feat1", "feat2"),
value = c(10,20, 1000, 2000))
featlist<-c("feat1", "feat2", "feat3")
test$feat <- factor(test$feat, levels = featlist)
test %>% spread(key = feat, value = value, fill = 0, drop = FALSE)
不幸的是,
tidyr::spread
不允许将自己的vector
用作键
,但幸运的是expand.grid
在调用spread
函数之前为您提供了使用自己的vector
和展开data.frame
的选项
library(tidyverse)
expand.grid(id=unique(test$id), feat = featlist) %>% #creates all combinations
mutate(feat = as.character(feat)) %>%
left_join(test, by=c("id", "feat")) %>% #Join with actual dataframe
spread(key=feat, value = value, fill = 0)
# id feat1 feat2 feat3
#1 1 10 20 0
#2 2 1000 2000 0
数据:
test<-data.frame(id=c(1,1,2,2),
feat=c("feat1", "feat2", "feat1", "feat2"),
value = c(10,20, 1000, 2000), stringsAsFactors = FALSE)
featlist<-c("feat1", "feat2", "feat3")
test不幸的是,tidyr::spread
不允许使用您自己的vector
作为键,但幸运的是expand.grid
为您提供了一个选项,在调用spread
函数之前使用您自己的vector
并展开data.frame
library(tidyverse)
expand.grid(id=unique(test$id), feat = featlist) %>% #creates all combinations
mutate(feat = as.character(feat)) %>%
left_join(test, by=c("id", "feat")) %>% #Join with actual dataframe
spread(key=feat, value = value, fill = 0)
# id feat1 feat2 feat3
#1 1 10 20 0
#2 2 1000 2000 0
数据:
test<-data.frame(id=c(1,1,2,2),
feat=c("feat1", "feat2", "feat1", "feat2"),
value = c(10,20, 1000, 2000), stringsAsFactors = FALSE)
featlist<-c("feat1", "feat2", "feat3")
test这是一个令人沮丧的简单解决方案。非常感谢。仅使用管道:test%>%mutate(feat=factor(feat,levels=featlist))%>%spread(feat,value,fill=0,drop=FALSE)
这是一个令人沮丧的简单解决方案。非常感谢。仅使用管道:test%>%mutate(feat=factor(feat,levels=featlist))%%>%spread(feat,value,fill=0,drop=FALSE)
请记住,我在回答中将列类型从factor
更改为character
。我想找到一个可以处理任何列类型的解决方案。请记住,在我的回答中,我已将列类型从factor
更改为character
。我想有一个可以处理任何列类型的解决方案。