Zhenpeng Hu
Zhenpeng Hu

Reputation: 13

How to put multiple variables inside the column as a factor in r

I have a problem when making the structure of the data.

enter image description here

lv1<-c(5,2,8)
lv2<-c(9,3,6)
lv3<-c(5,0,2)
df<-data.frame(lv1,lv2,lv3)

The result in the data is the number of people under lv1, lv2 and lv3. but I want to investigate the relationship between the number of people and different lv, so I would like to convert it look like this:

enter image description here

lv<- c('lv1','lv1','lv1','lv2','lv2','lv2','lv3','lv3','lv3')
number.of.people<-(c(5,2,8,9,3,6,5,0,2))
new.df<-data.frame(lv,number.of.people)

Is there any code I can do that?

Upvotes: 1

Views: 35

Answers (2)

G. Grothendieck
G. Grothendieck

Reputation: 269644

This uses no packages.

long <- stack(df)

giving:

> long
  values ind
1      5 lv1
2      2 lv1
3      8 lv1
4      9 lv2
5      3 lv2
6      6 lv2
7      5 lv3
8      0 lv3
9      2 lv3

If you want to change the names and reverse the column order then:

long <- stack(df)[2:1]
names(long) <- c("lv", "no.of.people")

Upvotes: 0

jyjek
jyjek

Reputation: 2707

You need tidyverse:

library(tidyverse)
    new.df%>%
     gather(lv,number.of.people)
   lv number.of.people
1 lv1                5
2 lv1                2
3 lv1                8
4 lv2                9
5 lv2                3
6 lv2                6
7 lv3                5
8 lv3                0
9 lv3                2

Upvotes: 2

Related Questions