我有一个包含3列(物种名称,经度和纬度)的csv文件和第二个包含物种名称列的csv文件。我想要做的是从第一个csv中提取与第二个csv文件中的物种匹配的物种(也就是长和列)。
我怎样才能在R?
中这样做答案 0 :(得分:3)
## Read csv files
file1 = read.csv(paste(path, "file1.csv", sep = ""), stringsAsFactors = FALSE, na.strings = "NA")
file2 = read.csv(paste(path, "file2.csv", sep = ""), stringsAsFactors = FALSE, na.strings = "NA")
#> file1
# Species Longitude Latitude
#1 Cat 0.4300052 0.04554442
#2 Dog 0.6568743 0.53359425
#3 Fish 0.8218709 0.20328321
#4 Deer 0.4601183 0.93191142
#5 Cow 0.9975495 0.02349226
#> file2
# Species
#1 Fish
#2 Dog
## Get subset in first file of species in second file
result = file1[file1$Species %in% file2$Species,]
你得到:
#> result
# Species Longitude Latitude
#2 Dog 0.6568743 0.5335943
#3 Fish 0.8218709 0.2032832