Al14
Al14

Reputation: 1814

Subset of a melt dataframe

My dataframe

a<-c(2, 4, 6, 6, 8, 10, 12, 13, 14)
c<-c(2, 2, 2, 2, 2, 2, 4, 4,4)
d<-c(10, 10, 10, 30, 30, 30, 50, 50, 50)
ID<-rep(c("no","bo", "fo"), each=3)
mydata<-data.frame(ID, a, c, d)

gg.df <- melt(mydata, id="ID", variable.name="variable")

I want to subset just the variable "no", I have tried:

gg.df[,"variable"=="no"]

which returns

data frame with 0 columns and 27 rows

Upvotes: 1

Views: 2293

Answers (1)

Andres Alvarado
Andres Alvarado

Reputation: 196

Please refer to the subset function:

no.df <- subset( x      = gg.df
               , subset = ID == "no" 
               )

 ID variable value
1  no        a     2
2  no        a     4
3  no        a     6
10 no        c     2
11 no        c     2
12 no        c     2
19 no        d    10
20 no        d    10
21 no        d    10

Or:

gg.df[ ID == "no",  ]

   ID variable value
1  no        a     2
2  no        a     4
3  no        a     6
10 no        c     2
11 no        c     2
12 no        c     2
19 no        d    10
20 no        d    10
21 no        d    10

Upvotes: 1

Related Questions