Natasha
Natasha

Reputation: 1521

Obtaining the index of elements that belong to common group in R

I have the following data frame,

>df
  Label
0 control1
1 control1
2 control2
3 control2
4 control1

To get the index of the elements with label control1 and control2, I do the following

Index1 <- grep("control1",df[,1])
Index2 <- grep("control2",df[,1])

In the above syntax, the labels control1 and control2 are explicity mentioned in the command.

Is there a way to find the labels automatically? The reason is the data frame, df,contents are parsed from different input files. For instance, I could have another data frame that reads

>df2
  Label
0 trol1
1 trol1
2 trol2
3 trol3
4 trol2

Is there a way to create a list of unique labels present in the column of df?

Upvotes: 2

Views: 335

Answers (3)

Chriss Paul
Chriss Paul

Reputation: 1101

You can try also

lapply(unique(df$Label), function(x) which(df$Label%in% x))

#with df
[[1]]
[1] 1 2 5

[[2]]
[1] 3 4

lapply(unique(df2$Label), function(x) which(df2$Label%in% x))
#with df2
[[1]]
[1] 1 2

[[2]]
[1] 3 5

[[3]]
[1] 4

Upvotes: 1

Silpara
Silpara

Reputation: 639

Using unique and which you can do:

df <- data.frame(Label = c("trol1", "trol1", "trol2", "trol3", "trol2"), stringsAsFactors=FALSE)
label_idx = list()
for(lbl in unique(df$Label)){
    label_idx[[lbl]] = which(df$Label == lbl)
}
label_idx

$`trol1`
[1] 1 2

$trol2
[1] 3 5

$trol3
[1] 4

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 389175

We can use split to get list of index according to unique Label

split(1:nrow(df), df$Label)

#$control1
#[1] 1 2 5

#$control2
#[1] 3 4

With df2

split(1:nrow(df2), df2$Label)

#$trol1
#[1] 1 2

#$trol2
#[1] 3 5

#$trol3
#[1] 4

Upvotes: 1

Related Questions