r/R_Programming • • Jul 08 '16

Wide to long?

My head does not want to grasp the wide vs long variables.. I have a df containing:

"2010"  "2011"  "2012"  "2013"  "2014"  "2015"
5007    4626        4563        4593        4677        5069

How do I make it 6 observations of 2 variables instead of the below?

> str(x)
'data.frame':   1 obs. of  6 variables:
1 Upvotes

6 comments sorted by

View all comments

Show parent comment

1

u/snicksn Jul 09 '16

Ah, I had changed the header=T in read.csv. Now it works.

> melt(x,id=c(NULL))
variable value
1    X2010  5007
2    X2011  4626 
3    X2012  4563
4    X2013  4593
5    X2014  4677
6    X2015  5069

Why is X added before year? How would I do the melting if I dont have header=T?

Trying to learn the melting but dont quite get it..

2

u/snicksn Jul 10 '16 edited Jul 10 '16

I find that:

x2 <- data.frame(stack(x[1,]), stack(x[2,]))

Also works (but you get two extra columns, $ind and $ind.1) But the simplest should be just saying column one should have the values of row one:

x2[,1] <- x[1,]
x2[,2] <- x[2,]

But it does not work:

Error in x2[1] <- x[1, ] : object of type 'closure' is not subsettable
> x2[,1] <- x[1,]

and I guess thats why the stacking is needed. I am curious how R keep track of different types, like I guess R knows if its a column or row? Sometimes I feel it might be easier to just manipulate text with loops.. but I guess thats due to my ignorance.

Thanks