Reputation: 7800
I have the following data frame:
df <- structure(list(Term = structure(c(6L, 2L, 4L, 3L, 1L, 5L), .Label = c("Cytokine-cytokine receptor interaction",
"cellular response to cytokine stimulus", "negative regulation of multi-organism process",
"positive regulation of biological process", "positive regulation of multicellular organismal process",
"regulation of viral process"), class = "factor"), c = c(10.698,
24.445, 0, 12.058, 9.542, 0), d = c(0, 7.093, 0, 0, 0, 0)), .Names = c("Term",
"c", "d"), class = "data.frame", row.names = c(154L, 147L, 105L,
157L, 86L, 104L))
That looks like this:
> df
Term c d
154 regulation of viral process 10.698 0.000
147 cellular response to cytokine stimulus 24.445 7.093
105 positive regulation of biological process 0.000 0.000
157 negative regulation of multi-organism process 12.058 0.000
86 Cytokine-cytokine receptor interaction 9.542 0.000
104 positive regulation of multicellular organismal process 0.000 0.000
What I want to do is given a list of vector:
target <- c("Cytokine-cytokine receptor interaction","negative regulation of multi-organism process ")
I want to select and sort the df$Term
based on target
.
Resulting in this data frame
Term c d
Cytokine-cytokine receptor interaction 9.542 0.000
negative regulation of multi-organism process 12.058 0.000
But why this command failed?
> df[match(target,df$Term),]
Term c d
86 Cytokine-cytokine receptor interaction 9.542 0
NA <NA> NA NA
What's the right way to do it?
Upvotes: 0
Views: 146
Reputation: 697
You erroneusly put a space at the end of
target <- c("Cytokine-cytokine receptor interaction","negative regulation of multi-organism process ") # <--- this space after process
hence the vectors don't match :)
Indeed, the following code does work:
df <- structure(list(Term = structure(c(6L, 2L, 4L, 3L, 1L, 5L),
.Label = c("Cytokine-cytokine receptor interaction",
"cellular response to cytokine stimulus",
"negative regulation of multi-organism process",
"positive regulation of biological process", "positive regulation of multicellular organismal process", "regulation of viral process"), class = "factor"), c = c(10.698,
24.445, 0, 12.058, 9.542, 0), d = c(0, 7.093, 0, 0, 0, 0)), .Names = c("Term",
"c", "d"), class = "data.frame", row.names = c(154L, 147L, 105L,
157L, 86L, 104L))
target <- c("Cytokine-cytokine receptor interaction","negative regulation of multi-organism process")
df[df$Term %in% target,]
Upvotes: 1