I would appreciate any help to create new variables from one variable in my actual dataset, which can be downloaded here.
Given this sample data:
dT<-structure(list(A = c("a1", "a2", "a1", "a1", "a2", "a1", "a1",
"a2", "a1"), B = c("b2", "b2", "b2", "b1", "b2", "b2", "b1",
"b2", "b1"), ID = c("3", "4", "3", "1", "4", "3", "1", "4", "1"
), E = c(0.621142094943352, 0.742109450696123, 0.39439152996948,
0.40694392882818, 0.779607277916503, 0.550579323666347, 0.352622183880119,
0.690660491345867, 0.23378944873769)), class = c("data.table",
"data.frame"), row.names = c(NA, -9L))
this code works to create several variables from the variable E
as expected:
library(data.table)
dcast(dT, A + B + ID ~ paste0("E", rowid(ID)))
# A B ID E1 E2 E3
#1 a1 b1 1 0.4069439 0.3526222 0.2337894
#2 a1 b2 3 0.6211421 0.3943915 0.5505793
#3 a2 b2 4 0.7421095 0.7796073 0.6906605
However, when I apply the same code to a larger dataset - available here, which is the actual data to which I want to apply the operation,
data.table
does not give the expected output as illustrated below (and available here) - this is the incorrect output:
library(readr)
mydata <- read_csv("mydata.csv")
library(data.table)
myDT<-dcast(mydata, A + B + ID ~ paste0("E", rowid(ID)))
View(myDT)
What I want is to be able to get this output (the incorrect output) from the larger dataset structured like the output that I get when I use a smaller dataset (the correct output).
I tried the solutions discussed here and here but these did not work for my case as discussed here.
Thanks in advance for any help.