将数据框列转换为命名的数字向量

3

我有一个数据框 blah:

blah <- data.frame(x=c("Red", "Blood Red", "Crimson", "Maroon"), y=c(20, 1, 14, 13))

我希望将blah转换为命名的数字/成员向量:
blah <- c(`Red` = 20, `Blood Red` = 1, `Crimson` = 14, `Maroon` = 13)

其中x是名称,y是值。

我该怎么做才能实现这个?


使用案例:https://github.com/hrbrmstr/waffle - emehex
1个回答

5

只需使用 setNames 即可。

setNames(blah$y, blah$x)
# Red Blood Red   Crimson    Maroon 
#  20         1        14        13 

1
R是最好的。9分钟后接受。谢谢! - emehex

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接