[英]R Row selection vector from data frame
I want to implement a simple row selection vector using the 'country' column on the below lat_all data frame in R. 我想使用R中下面的lat_all数据框中的“country”列实现一个简单的行选择向量。
pop country lat
12.4 Afghanistan 62
24.3 Australia 12
4 New Zealand 10
100 Japan 30
12 Fiji 28
Using a selection below which I can change easily such as 使用下面的选项,我可以很容易地改变,如
selection <- c("Afghanistan","Australia","Japan")
I want the result to be: 我希望结果如下:
pop country lat
1 12.4 Afghanistan 62
2 24.3 Australia 12
4 100 Japan 30
Only way I can get what I want so far is using the below which doesn't use the selection vector 到目前为止,我能得到我想要的只是使用下面不使用选择向量的方法
lat_all[ lat_all[2]==("Afghanistan") | lat_all[2]==("Australia") | lat_all[2]==("Japan"), ]
I don't understand why something like lat_all[2]==selection
doesn't work for me. 我不明白为什么像
lat_all[2]==selection
这样的东西对我不起作用。 The selection vector only finds Afghanistan and Japan. 选择向量只能找到阿富汗和日本。 Australia is FALSE and any output using that vector skips Australia.
澳大利亚是假的,任何使用该矢量的输出都会跳过澳大利亚。
use subset function
lat_all <- subset(lat_all, country %in% selection)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.