3
dt <- data.frame(means = runif(5),
           sds = runif(5),
           medians = runif(5),
           MissedPercentage = sample(c(0.1,0.3),5,replace = T) ) 

i wish to gather all the means,sds,and medians, but the MissedPercentage. So, here is what i did:

dt<-gather(dt,key=stat,value=c(`means`,`sds`,`medians`))

but it complains with:

Error: Must supply a symbol or a string as argument

My favorit answer is:

   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
Jeff
  • 7,767
  • 28
  • 85
  • 138

2 Answers2

2

Just do:

tidyr::gather(dt,stat,value,1:3,-4)
NelsonGon
  • 13,015
  • 7
  • 27
  • 57
  • 1
    For me only this solution works, Humpelstielzchen 's gives the same error. `tidyr * 1.1.2 2020-08-27 [1] CRAN (R 4.0.2)` with param names `tidyr::gather(data = dt, key = stat, value = value,1:3,-4)` – bud.dugong Jan 18 '21 at 23:37
0
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

Humpelstielzchen
  • 6,126
  • 3
  • 14
  • 34