B.Mr.W.
B.Mr.W.

Reputation: 19638

R "melt-cast" like operation

I have a file contains contents like this:

name: erik
age: 7
score: 10
name: stan
age:8
score: 11
name: kyle 
age: 9
score: 20
...

As you can see, each record actually contains 3 rows in the file. I am wondering how can I read in the file and transform into data dataframe looks like below:

name    age    score
erik    7      10
stan    8      11
kyle    9      20
...

What I have done so far(thanks tcash21):

> data <- read.table(file.choose(), header=FALSE, sep=":", col.names=c("variable", "value"))
> data
variable  value
1     name   erik
2      age      7
3    score     10
4     name   stan
5      age      8
6    score     11
7     name  kyle 
8      age      9
9    score     20

I am thinking how can I split the column into two columns by : and then maybe use something similar like cast in reshape package to do what I want? or how can I get the rows that has index number 1,4,7,... only, which has a constant step

Thanks!

Upvotes: 0

Views: 181

Answers (2)

Henrik
Henrik

Reputation: 67778

Another possibility:

library(reshape2)
df$id <- rep(1:(nrow(df)/3), each = 3)
dcast(df, id ~ variable, value.var = "value")

#   id age  name score
# 1  1   7  erik    10
# 2  2   8  stan    11
# 3  3   9  kyle    20

Upvotes: 1

Josh
Josh

Reputation: 1

If the format is predictable you might want to do something really simple like

# recreate data
data <- as.matrix(c("erik",7,10,"stan",8, 11,"kyle",9,20),ncol=1)

# get individual variables
names <- data[seq(1,length(data)-2,3)]
age <- data[seq(2,length(data)-1,3)]
score <- data[seq(3,length(data),3)]

# combine variables
reformatted.data <- as.data.frame(cbind(names,age,score))

Upvotes: 0

Related Questions