Reputation: 101
Okay, this seems like it is probably somewhat straightforward, but I am an R noob and can't seem to figure it out.
I read in a table from a csv file with two columns, where the first column is a location name followed by the value of an observation taken at that location. Only two locations exist, but there are many samples taken at each location which are in the second column. I want to transform the table so that the locations become the column header, and the desired number of samples are stacked under the proper location by index range.
The script below shows what I want to do.
Thanks, in advance, for helping.
df <- data.frame(col1 = rep(c("A","B"), each = 5), col2 = 1:10)
df2 <- acast(df, col2 ~ col1, value.var = "col2")
Current output has a bunch of NA values in it. Output
Desired output is:
# A B
1 6
2 7
3 8
4 9
5 10
Upvotes: 0
Views: 77
Reputation: 523
Try this:
df <- data.frame(col1 = rep(c("A","B", "C"), each = 5), col2 = 1:15)
do.call(cbind, split(df$col2, df$col1 ))
# col1 col2
# 1 A 1
# 2 A 2
# 3 A 3
# 4 A 4
# 5 A 5
# 6 B 6
# 7 B 7
# 8 B 8
# 9 B 9
# 10 B 10
# 11 C 11
# 12 C 12
# 13 C 13
# 14 C 14
# 15 C 15
# A B C
# [1,] 1 6 11
# [2,] 2 7 12
# [3,] 3 8 13
# [4,] 4 9 14
# [5,] 5 10 15
if you need the first N observation, say 3, you can do this:
do.call(cbind, split(df$col2, df$col1 ))[1:3, ]
Upvotes: 1