dt <- data.frame(means = runif(5),
sds = runif(5),
medians = runif(5),
MissedPercentage = sample(c(0.1,0.3),5,replace = T) )
我希望收集所有means
,sds
和medians
,但MissedPercentage
。所以,这就是我所做的:
dt<-gather(dt,key=stat,value=c(`means`,`sds`,`medians`))
但它抱怨:
Error: Must supply a symbol or a string as argument
我最喜欢的答案是:
stat c("means", "sds", "medians") MissedPercentage
1 means 0.12638209 0.1
2 means 0.80834248 0.1
3 means 0.94386651 0.3
4 means 0.87709217 0.3
5 means 0.30200943 0.3
6 sds 0.06297956 0.1
答案 0 :(得分:1)
dt <- data.frame(means = runif(5),
sds = runif(5),
medians = runif(5),
MissedPercentage = sample(c(0.1,0.3),5,replace = T) )
dt<-gather(dt,key=stat, value=c('means','sds','medians'), -MissedPercentage) %>%
select(2,3,1)
>head(dt)
stat c("means", "sds", "medians") MissedPercentage
1 means 0.9677432 0.1
2 means 0.7025229 0.1
3 means 0.2191380 0.3
4 means 0.6291055 0.3
5 means 0.3078163 0.1
6 sds 0.5811671 0.1
答案 1 :(得分:1)
只需:
tidyr::gather(dt,stat,value,1:3,-4)