Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Change orientation of data frame for panel data in R [duplicate]

$
0
0

This question already has an answer here:

I would like to change the orientation of my data frame.

I have following data:

df <- data.frame(name=c("Anna","Ben","Charlie"),
                 "year2017"=c("1", "2", "3"),
                 "year2018"=c("4", "1", "2"),
                 "year2019"=c("4", "2", "1"),
                 stringsAsFactors = F)

The data frame I would like to create should look like that:

name     year
Anna     1
Anna     4
Anna     4
Ben      2
Ben      1
Ben      2
Charlie  3
Charlie  2
Charlie  1

Is there a way to get to this alignment of the data? Thanks in advance!


Viewing all articles
Browse latest Browse all 201839

Trending Articles