How to do vlookup and fill down (like in Excel) in R?

前端 未结 8 927
悲&欢浪女
悲&欢浪女 2020-11-22 11:25

I have a dataset about 105000 rows and 30 columns. I have a categorical variable that I would like to assign it to a number. In Excel, I would probably do something with

8条回答
  •  清歌不尽
    2020-11-22 11:46

    I also like using qdapTools::lookup or shorthand binary operator %l%. It works identically to an Excel vlookup, but it accepts name arguments opposed to column numbers

    ## 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
    

提交回复
热议问题