First, I want to convert this data:
datinput = read.table(header = TRUE, text = "
var1 var2 var3
A 3 10
B 2 6
")
datinput
var1 var2 var3
1 A 3 10
2 B 2 6
into this format:
datoutput = read.table(header = TRUE, text = "
var2.A var3.A Var2.B var3.B
3 10 2 6
")
var2.A var3.A Var2.B var3.B
1 3 10 2 6
I tried reshape2::dcast
, but does not deliver the desired output.
Instead, dcast gives this:
datinput%>%reshape2::dcast(var1~var2, value.var="var3")
var1 2 3
1 A NA 10
2 B 6 NA
datinput%>%reshape2::dcast(var1, value.var=c("var2", "var3"))
Error in is.formula(formula) : object 'var1' not found
datinput%>%reshape2::dcast(var1~var1, value.var=c("var2", "var3"))
Error in .subset2(x, i, exact = exact) : subscript out of bounds
In addition: Warning message:
In if (!(value.var %in% names(data))) { :
the condition has length > 1 and only the first element will be used
Then, I want to make the names_from come first in the new names.
I want to have these new columns named A.var2 B.var2 A.var3 B.var3. This is because I want to arrange the resulting variables using the variable names by alphabetical order into A.var2 A.var3 B.var2 B.var3
Thanks for any help.
We can use pivot_wider
library(dplyr)
library(tidyr)
library(stringr)
datinput %>%
pivot_wider( names_from = var1, values_from = c('var2', 'var3'), names_sep=".") %>%
rename_all(~ str_replace(., '^(.*)\\.(.*)', '\\2.\\1'))
The dcast
from reshape2
do not use multiple value columns. Instead, it can be done with data.table::dcast
library(data.table)
dcast(setDT(datinput), rowid(var1) ~ var1, value.var = c("var2", "var3"), sep=".")
# var1 var2.A var2.B var3.A var3.B
#1: 1 3 2 10 6