ultimately, I need to search column 1 of my data frame and if I find a match to var
, then I need to get the element next to it in the same row (the next column over)
I thought I could usematch
or %in%
to find the index in a vector I got from the data frame, but I keep getting NA
one example I looked at is Is there an R function for finding the index of an element in a vector?, and I don't understand why my code is any different from the answers.
so in my code below, if I find b
in column 1, I eventually want to get 20 from the data frame.
What am I doing wrong? I would preferr to stick to r-base
packages if possible
> df = data.frame(names = c("a","b","c"),weight = c(10,20,30),height=c(5,10,15))
> df
names weight height
1 a 10 5
2 b 20 10
3 c 30 15
> vect = df[1]
> vect
names
1 a
2 b
3 c
> match("b", vect)
[1] NA