Reputation: 3
I have a text file that has one colunm of 1800 entries that are actually two sets of data (size,age) that have been combined. The first number (row1,column1) is size 1, the second number (row2,column1) is age1, the third column (row3,column1) is size 2, the fourth number (row4,column1) is age2, and so on. Does anyone know how to split this into a matrix of two columns of 900 entries each?
Upvotes: 0
Views: 2070
Reputation: 41990
If you have a single vector x
containing your alternating size and age data:
df <- do.call(data.frame, split(x, c("size", "age")))
If you want 2-column matrix instead, use cbind
:
df <- do.call(cbind, split(x, c("size", "age")))
This isn't the most efficient way, but it does automatically give you column names for free.
Upvotes: 1
Reputation: 58825
You can make a matrix out of it and do it that way.
matrix(DF[,1], ncol=2, byrow=TRUE)
where DF
is the data.frame you read the data into.
Upvotes: 1
Reputation: 23898
Suppose you have data.frame
df. Then try this
Size <- df[seq(from = 1, to = nrow(df), by = 2), 1]
Age <- df[seq(from = 2, to = nrow(df), by = 2), 1]
dfNew <- data.frame(Size, Age)
Upvotes: 2