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.