使用R中的查找表替换列值时出现问题

时间:2015-09-03 19:13:20

标签: r dataframe dplyr

我想将列值替换为lut中定义的列值:

library("hflights")
library(dplyr)  # For tbl_df
hflights <- tbl_df(hflights)

glimpse(hflights)

lut <- c("AA" = "American", "AS" = "Alaska", "B6" = "JetBlue", "CO" = "Continental", "DL" = "Delta", "OO" = "SkyWest", "UA" = "United", "US" = "US_Airways", "WN" = "Southwest", "EV" = "Atlantic_Southeast", "F9" = "Frontier", "FL" = "AirTran", "MQ" = "American_Eagle", "XE" = "ExpressJet", "YV" = "Mesa")

carriers <- hflights$UniqueCarrier

carriers <- lut[carriers]

hflights$UniqueCarrier <- carriers

glimpse(hflights)

lut <- c ("A" = "carrier", "B" = "weather", "C" = "FFA", "D" = "security", "E" = "not cancelled")


cancellation <- hflights$CancellationCode
cancellation <- lut[cancellation]
hflights$CancellationCode <- cancellation

glimpse(hflights)

当我使用hflights <- tbl_df(hflights)时,两个替换操作都失败,我得到相应列的NA值,但是当我不使用tbl包中的dplyr时,第一个一个是成功的但是对于第二个我得到NA值。 hflights是包含休斯顿航班数据的包。我怎么了?抱歉我的英语不好!

0 个答案:

没有答案