Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I extract values from a data.frame based on a vector in R?

suppose I have a numeric vector like:

x <- c(1.0, 2.5, 3.0)

and data.frame:

df<-data.frame(key=c(0.5,1.0,1.5,2.0,2.5,3.0),
       value=c(-1.187,0.095,-0.142,-0.818,-0.734,0.511))

df
  key  value
1 0.5 -1.187
2 1.0  0.095
3 1.5 -0.142
4 2.0 -0.818
5 2.5 -0.734
6 3.0  0.511

I want to extract all the rows in df$key that have the same values equal to x, with result like:

df.x$value
[1] 0.095 -0.734  0.511

Is there an efficient way to do this please? I've tried data.frame, hash package and data.table, all with no success. Thanks for help!


Thanks guys. I actually tried similar thing but got df$key and x reversed. Is it possible to do this with the hash() function (in the 'hash' package)? I see hash can do things like:

h <- hash( keys=letters, values=1:26 )
h$a # 1

h$foo <- "bar"
h[ "foo" ]
h[[ "foo" ]]

z <- letters[3:5]

h[z]
<hash> containing 3 key-value pair(s).
c : 3
d : 4
e : 5

But seems like it doesn't take an array in its key chain, such as:

h[[z]]
Error in h[[z]] : wrong arguments for subsetting an environment

but I need the values only as in a vector rather than a hash. Otherwise, it would be perfect so that we can get rid of data.frame by using some 'real' hash concept.

like image 444
Rock Avatar asked Oct 31 '11 08:10

Rock


People also ask

How do I extract values from a vector in R?

Vectors are basic objects in R and they can be subsetted using the [ operator. The [ operator can be used to extract multiple elements of a vector by passing the operator an integer sequence.

How do you subset a DataFrame based on a vector in R?

If we have a vector and a data frame, and the data frame has a column that contains the values similar as in the vector then we can create a subset of the data frame based on that vector. This can be done with the help of single square brackets and %in% operator.


2 Answers

Try,

df[df$key %in% x,"value"] # resp
df[df$key %in% x,]

Using an OR | condition you may modify it in such a way that your vector may occur in either of your columns. General tip: also have a look at which.

like image 174
Matt Bannert Avatar answered Nov 15 '22 11:11

Matt Bannert


Have you tried testing the valued of df$key that are in x and extracting the value in the value column? I only say this out loud because StackOverflow doesnt like oneline answers:

> x
[1] 1.0 2.5 3.0
> df
  key      value
1 0.5 -0.7398436
2 1.0  0.6324852
3 1.5  1.8699257
4 2.0  1.0038996
5 2.5  1.2432679
6 3.0 -0.6850663
> df[df$key %in% x,'value']
[1]  0.6324852  1.2432679 -0.6850663
> 

BIG WARNING - comparisons with floating point numbers with == can be a bad idea - read R FAQ 7.31 for more info.

like image 22
Spacedman Avatar answered Nov 15 '22 11:11

Spacedman