简体   繁体   English

如何根据R中的向量从data.frame中提取值?

[英]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: 和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 $ key中具有相同值等于x的所有行,结果如下:

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. 我已经尝试过data.frame,hash包和data.table,但都没有成功。 Thanks for help! 感谢帮助!


Thanks guys. 多谢你们。 I actually tried similar thing but got df$key and x reversed. 我实际上尝试了类似的东西,但得到了df $ key和x反转。 Is it possible to do this with the hash() function (in the 'hash' package)? 是否可以使用hash()函数(在'hash'包中)执行此操作? 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. 否则,它将是完美的,以便我们可以通过使用一些“真正的”哈希概念来摆脱data.frame。

Try, 尝试,

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

Using an OR | 使用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 . 一般提示:还看看which

Have you tried testing the valued of df$key that are in x and extracting the value in the value column? 您是否尝试过测试x中的df $ key值并提取值列中的值? I only say this out loud because StackOverflow doesnt like oneline answers: 我只是大声说出来因为StackOverflow不喜欢oneline答案:

> 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. 大警告 - 使用==与浮点数进行比较可能是一个坏主意 - 阅读R FAQ 7.31了解更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM