潇湘沐
如果我正确理解了您的问题,下面是四种方法来完成与Excel相同的操作VLOOKUP然后用R:# load sample data from Qhous <- read.table(header = TRUE,
stringsAsFactors = FALSE, text="HouseType HouseTypeNo
Semi 1
Single 2
Row 3
Single 2
Apartment 4
Apartment 4
Row 3")# create a toy large table with a 'HouseType' column # but no 'HouseTypeNo' column (yet)largetable <- data.frame(
HouseType = as.character(sample(unique(hous$HouseType), 1000, replace = TRUE)), stringsAsFactors = FALSE)# create a lookup table to get t
he numbers to fill# the large tablelookup <- unique(hous)
HouseType HouseTypeNo1 Semi 12 Single 23 Row 35 Apartment
4下面是四种方法来填充HouseTypeNo在largetable中的值。lookup表:先与merge基地:# 1. using base base1 <- (merge(lookup, largetable, by = 'HouseType'))第二种方法,基中有命名向量:# 2. using base and a named vectorhousenames <- as.numeric(1:length(unique(hous$HouseType)))names(housenames) <- unique(hous$HouseType)base2
<- data.frame(HouseType = largetable$HouseType,
HouseTypeNo = (housenames[largetable$HouseType]))第三,使用plyr一揽子:# 3. using the plyr packagelibrary(plyr)plyr1 <- join(largetable, lookup, by = "HouseType")第四,使用sqldf包装# 4. using the sqldf packagelibrary(sqldf)sqldf1 <- sqldf("SELECT largetable.HouseType, lookup.HouseTypeNo
FROM largetable
INNER JOIN lookup
ON largetable.HouseType = lookup.HouseType")如果有可能有些人在largetable不存在于lookup然后使用左联接:sqldf("select * from largetable left join lookup using (HouseType)")对其他解决方案也需要相应的修改。这就是你想做的吗?让我知道你喜欢哪种方法,我会添加评论。
不负相思意
我也喜欢用qdapTools::lookup或速记二进制运算符%l%..它的工作原理与ExcelVLOOKUP相同,但它接受与列号相反的名称参数。## Replicate Ben's data:hous <- structure(list(HouseType = c("Semi", "Single", "Row", "Single",
"Apartment", "Apartment", "Row"), HouseTypeNo = c(1L, 2L, 3L,
2L, 4L, 4L, 3L)), .Names = c("HouseType", "HouseTypeNo"),
class = "data.frame", row.names = c(NA, -7L))largetable <- data.frame(HouseType = as.character(sample(unique(hous$HouseType),
1000, replace = TRUE)), stringsAsFactors = FALSE)## It's this simple:library(qdapTools)largetable[, 1] %l% hous