Let's say I got a table like this:
data <- c(1,2,3,6,5,6,9,"LC","LC","HC","HC","LC","HC","ALL")
attr(data,"dim") <- c(7,2)
data
[,1] [,2]
[1,] "1" "LC"
[2,] "2" "LC"
[3,] "3" "HC"
[4,] "6" "HC"
[5,] "5" "LC"
[6,] "6" "HC"
[7,] "9" "ALL"
Now I want to manipulate the data so it looks like this:
[,"LC"] [,"HC"] [,"ALL"]
[1,] "1" "3" "9"
[2,] "2" "6"
[3,] "5" "6"
Is there a way to do this in R or is it just impossible and should I try another way of getting access to my data?
You can get very close by using
split
. This returns a list with the values you wanted and you can then uselapply
or any other list manipulation function:If you must have the output in matrix format, then I suggest you pad the short vectors with NA:
This results in:
EXAMPLE DATA
I prefere to read data into data.frame as it check if vectors are of equal length.
CODE