第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在R中完成VLOOKUP和填充(如Excel)?

如何在R中完成VLOOKUP和填充(如Excel)?

如何在R中完成VLOOKUP和填充(如Excel)?我有一個(gè)大約105000行30列的數(shù)據(jù)集。我有一個(gè)分類變量,我想把它分配給一個(gè)數(shù)字。在Excel中,我可能會(huì)用VLOOKUP然后填滿。我要怎么做同樣的事R?本質(zhì)上,我所擁有的是HouseType變量,我需要計(jì)算HouseTypeNo..以下是一些樣本數(shù)據(jù):HouseType HouseTypeNoSemi            1Single          2Row             3Single          2Apartment       4Apartment       4Row             3
查看完整描述

3 回答

?
瀟湘沐

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超6個(gè)贊

如果我正確理解了您的問題,下面是四種方法來完成與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

下面是四種方法來填充HouseTypeNolargetable中的值。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然后使用左聯(lián)接:

sqldf("select * from largetable left join lookup using (HouseType)")

對(duì)其他解決方案也需要相應(yīng)的修改。

這就是你想做的嗎?讓我知道你喜歡哪種方法,我會(huì)添加評(píng)論。


查看完整回答
反對(duì) 回復(fù) 2019-07-11
?
手掌心

TA貢獻(xiàn)1942條經(jīng)驗(yàn) 獲得超3個(gè)贊

我想你也可以用match():

largetable$HouseTypeNo <- with(lookup,
                     HouseTypeNo[match(largetable$HouseType,
                                       HouseType)])

如果我按…的順序排列,這仍然有效。lookup.


查看完整回答
反對(duì) 回復(fù) 2019-07-11
?
不負(fù)相思意

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超10個(gè)贊

我也喜歡用qdapTools::lookup或速記二進(jìn)制運(yùn)算符%l%..它的工作原理與ExcelVLOOKUP相同,但它接受與列號(hào)相反的名稱參數(shù)。

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


查看完整回答
反對(duì) 回復(fù) 2019-07-11
  • 3 回答
  • 0 關(guān)注
  • 898 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)