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

发布于 2024-12-12 12:00:40 字数 1072 浏览 0 评论 0原文

假设我有一个像这样的数字向量:

x <- c(1.0, 2.5, 3.0)

和 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

我想提取 df$key 中具有相同值等于 x 的所有行,结果如下:

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

有没有一种有效的方法可以做到这一点?我尝试过 data.frame、hash 包和 data.table,但都没有成功。感谢您的帮助!


谢谢你们。我实际上尝试过类似的事情,但 df$key 和 x 颠倒了。是否可以使用 hash() 函数(在 '哈希'包)?我看到哈希可以做这样的事情:

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

但似乎它的钥匙链中没有数组,例如:

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

但我只需要向量而不是哈希中的值。否则,这将是完美的,这样我们就可以通过使用一些“真正的”哈希概念来摆脱 data.frame 。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌逼全场 2024-12-19 12:00:40

尝试

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

使用 OR | 条件,您可以修改它,使向量可以出现在任一列中。一般提示:还可以查看 which

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.

没有你我更好 2024-12-19 12:00:40

您是否尝试过测试 x 中 df$key 的值并提取值列中的值?我只是大声说出来,因为 StackOverflow 不喜欢单行答案:

> 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
> 

大警告 - 用 == 与浮点数比较可能是一个坏主意 - 请阅读 R FAQ 7.31 了解更多信息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文