使用查找表填充多个列

时间:2014-09-18 16:11:30

标签: r lookup

我有九个不同的数据帧,我想在其上运行相同的查找测试。

以下是九个数据帧中的两个......

PIRO_PLS_Points <- structure(list(SPECIES1 = c("CE", "BE", "BE", "YB", "BF", "BE", 
"SU", "CE", "SU", "SU"), ComName1 = c(NA_character_, NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, 
NA_character_, NA_character_, NA_character_), Latin1 = c(NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_), 
    TSN1 = c(NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_), SPECIES2 = c("CE", "BE", "BE", 
    "YB", "BF", "BE", "BE", "SU", "SU", "SU"), ComName2 = c(NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_), Latin2 = c(NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_
    ), TSN2 = c(NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_), SPECIES3 = c(NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_), ComName3 = c(NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_
    ), Latin3 = c(NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_), TSN3 = c(NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_)), data_types = c("N", "N", "F", "F", "N", 
"N", "N", "F", "F", "C", "C", "N", "C", "C", "C", "C", "C", "C", 
"N", "C", "C", "C", "C", "C", "C", "C", "C", "C", "N", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "N", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "N", "C", "C", "C", "C", "C", "N", "C", "N", 
"N", "N", "C", "N", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"N", "N", "C", "F", "F", "F", "F", "N", "N", "F", "F", "N", "N", 
"C", "C", "C", "C", "F", "F", "C"), .Names = c("SPECIES1", "ComName1", 
"Latin1", "TSN1", "SPECIES2", "ComName2", "Latin2", "TSN2", "SPECIES3", 
"ComName3", "Latin3", "TSN3"), row.names = c("1", "2", "3", "4", 
"5", "6", "7", "8", "9", "10"), class = "data.frame")

SLBE_PLS_Points <- structure(list(SPECIES1 = c("CE", "BE", "BE", "YB", "BF", "BE", 
"SU", "CE", "SU", "SU"), ComName1 = c(NA_character_, NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, 
NA_character_, NA_character_, NA_character_), Latin1 = c(NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_), 
    TSN1 = c(NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_), SPECIES2 = c("CE", "BE", "BE", 
    "YB", "BF", "BE", "BE", "SU", "SU", "SU"), ComName2 = c(NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_), Latin2 = c(NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_
    ), TSN2 = c(NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_), SPECIES3 = c(NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_), ComName3 = c(NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_
    ), Latin3 = c(NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_), TSN3 = c(NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_)), data_types = c("N", "N", "F", "F", "N", 
"N", "N", "F", "F", "C", "C", "N", "C", "C", "C", "C", "C", "C", 
"N", "C", "C", "C", "C", "C", "C", "C", "C", "C", "N", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "N", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "N", "C", "C", "C", "C", "C", "N", "C", "N", 
"N", "N", "C", "N", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"N", "N", "C", "F", "F", "F", "F", "N", "N", "F", "F", "N", "N", 
"C", "C", "C", "C", "F", "F", "C"), .Names = c("SPECIES1", "ComName1", 
"Latin1", "TSN1", "SPECIES2", "ComName2", "Latin2", "TSN2", "SPECIES3", 
"ComName3", "Latin3", "TSN3"), row.names = c("1", "2", "3", "4", 
"5", "6", "7", "8", "9", "10"), class = "data.frame")

数据框已根据另一列包含我要填充的列(ComName1Latin1TSN1ComName2等); SPECIES1SPECIES2

这是我想要应用的查找表...

sp.list <-c('BE','YB','SU')
com.list <-c('bear','yellow','supper')
latin.list <- c('juglans','quercus','fraxinus')
tsn.list <- c(100203,999999,88888)

lookup = cbind(sp.list,com.list,latin.list,tsn.list)

看起来像:

     sp.list com.list latin.list tsn.list
[1,] "BE"    "bear"   "juglans"  "100203"
[2,] "YB"    "yellow" "quercus"  "999999"
[3,] "SU"    "supper" "fraxinus" "88888"

所以,最后我想填充$ComName1$Latin1$TSN1$ComName2等等,所有这些都基于lookup表。我认为查找表是最简单的方法,但我对其他建议持开放态度。

1 个答案:

答案 0 :(得分:1)

最简单的事情可能是堆叠所有数据帧,然后在查找表上合并

dfs <- rbind(PIRO_PLS_Points, SLBE_PLS_Points, ...)
dfs$sp.list <- dfs$SPECIES1
merged <- merge(dfs, lookup)

您可以使用split()或子集将其拆分为单独的数据框。