Reputation: 11
I need to take a two column vector in r. In the first column I have dates, 12/21/2011 format and I have a number in the second column, 255 format.
I need to take a number of my choosing say 255 and see if it matches any numbers in my second column. If it does match I need to return the date it matched on.
I know about match, count, in etc. I just cannot seem to put it together. I am a newbie perhaps this is a bit beyond my ability but I figure that if I learn something I'll be that much better for it.
There are some partial matches in the questions but nothing as detailed as what I want.
If anyone has any examples that will teach me I'd be more than happy. If you have a reference in a book I will do that myself if you tell me the reference.
Thank you very much. I'm using R 2.13.1 in a Windows XP SP3 environment.
Upvotes: 1
Views: 3030
Reputation: 49640
Also consider using merge
, have your column of lookup values and your column of dates in a data frame, then merge this with another data frame that has the value that you want to look up (or a bunch of values that you want to look up). By default it will return a data frame with only the values from both groups that match, you can set the arguments to keep those that don't match and they will have missing values to show that they did not match.
Upvotes: 0
Reputation: 3148
Getting acquainted with indexing in R will help you with this task (and many others), without the need for additional functions. To select only certain rows and columns in a matrix or dataframe, the format is x[rows,columns]
, where leaving either rows
or columns
blank displays all.
In your case, this is what we could do. First, let's create an example matrix (note that a '2 column vector' is actually a matrix):
x <- cbind(c("12/11/11", "12/10/11", "10/16/11",
"11/07/11"), c(1, 255, 3, 255))
# [,1] [,2]
#[1,] "12/11/11" "1"
#[2,] "12/10/11" "255"
#[3,] "10/16/11" "3"
#[4,] "11/07/11" "255"
Using a logical vector in your row index, you can return only the rows that contain a certain value. For instance, here's a logical vector for any row where column 2 = 255:
x[,2] == 255
#[1] FALSE TRUE FALSE TRUE
Inserting this logical vector into your row index will return only rows labeled TRUE
.
x[x[,2]==255,]
# [,1] [,2]
#[1,] "12/10/11" "255"
#[2,] "11/07/11" "255"
To show only the dates, specify column 1 in your index:
x[x[,2]==255,1]
#[1] "12/10/11" "11/07/11"
Upvotes: 2
Reputation: 28159
You might want to look at subset().
> x1 <- rnorm(20)*10
> y1 <- rnorm(20)*5
> z1 <- cbind(round(abs(x1),0), round(abs(y1),0)) ## just creates 2 columns of data.
> z1
[,1] [,2]
[1,] 9 1
[2,] 6 6
[3,] 3 7
[4,] 10 0
[5,] 9 2
[6,] 7 7
[7,] 7 10
[8,] 3 1
[9,] 6 10
[10,] 6 5
[11,] 0 11
[12,] 5 0
[13,] 0 8
[14,] 2 4
[15,] 1 2
[16,] 3 3
[17,] 9 7
[18,] 12 4
[19,] 1 1
[20,] 6 3
> ss1 <- subset(z1, z1[,2]==2) ## creates subset of 'z1' where column 2 equals 2.
> ss1 ## shows contents of ss1
[,1] [,2]
[1,] 9 2
[2,] 1 2
Upvotes: 0