Search through a table in R?
I'm new to using R and I'm trying to find something from a table. I have a table that is read using read.table(), like this:
A 1
B 2
C 3
D 4
Given a query, say "C", I need to find the corresponding element from the secon开发者_StackOverflow中文版d column. How can I iterate through the elements in the first column to find which row contains "C", then extract "3" from the second column?
Thanks! :)
It depends whether the first column shown above is really a column or whether it represents the row names.
If your data object is X
then if the former (no row names):
X[X[,1]=="C",2]
or if the latter (row names)
X["C",2]
The stuff before the comma indexes the rows, the stuff after the comma indexes the columns. With respect, this question indicates that it would be extremely valuable to go read the "Introduction to R" that comes with R (or almost any other basic R text) ...
精彩评论