My data frame looks like this:
x <- data.frame(c("a","a","a","a","b","b","c","c","c","a", "a"), c(1,2,3,4,1,2,1,2,3, 1, 2))
names(x) <- c("id","nr")
id nr
1 a 1
2 a 2
3 a 3
4 a 4
5 b 1
6 b 2
7 c 1
8 c 2
9 c 3
10 a 1
11 a 2
I want to have something like this:
id 1 2 3 4
a 1 2 3 4
a 1 2 NA NA
b 1 2 NA NA
c 1 2 3 NA
I already used dcast(x, id ~ nr, value.var ="nr")
but I got the warning:
"Aggregation function missing: defaulting to length".
I understand that this is due to non-unique rows. Also I created groups which give me the results above. But is there a way to create it without having to create groups?
x <- data.frame(c("a","a","a","a","b","b","c","c","c","a", "a"),
c(1,1,1,1,1,1,1,1,1,2,2), c(1,2,3,4,1,2,1,2,3, 1, 2))
names(x) <- c("id", "group","nr")
dcast(x, id + group ~ nr, value.var = "nr")