在R中使用查找表将所有变量的类转换为因子

时间:2015-11-29 19:23:04

标签: r lookup

我想使用查找表来查找和替换数据框中的匹配值,但是当我应用查找表时,它会将数据框中的所有变量更改为factor。有没有办法在不更改变量类的情况下应用此查找表?

这是我的数据:

df <- structure(list(year = c(2008, 2008, 2008, 2010, 2009, 2009, 2011, 
2007, 2011, 2009, 2007, 2008, 2010, 2006, 2009, 2010, 2009, 2006, 
2009, 2008), change_occurred = c("true", "false", "true", "false", 
"false", "true", "false", "false", "false", "false", "false", 
"false", "true", "false", "false", "true", "false", "false", 
"false", "false"), agent_01 = c("harvest", "none", "development", 
"none", "none", "agriculture", "none", "none", "none", "none", 
"none", "none", "insect_disease_defo", "none", "none", "insect_disease_defo", 
"none", "none", "none", "none"), agent_01_conc = c("harvest_60", 
"none", "development", "none", "none", "agriculture", "none", 
"none", "none", "none", "none", "none", "insect_disease_defo", 
"none", "none", "insect_disease_defo", "none", "none", "none", 
"none"), ha_affect = c(3.87, 0, 1.134, 0, 0, 1.44, 0, 0, 0, 0, 
0, 0, 1.8, 0, 0, 2.43, 0, 0, 0, 0)), .Names = c("year", "change_occurred", 
"agent_01", "agent_01_conc", "ha_affect"), row.names = c(NA, 
20L), class = "data.frame")

df的结构:

str(df)
'data.frame':   20 obs. of  5 variables:
 $ year           : num  2008 2008 2008 2010 2009 ...
 $ change_occurred: chr  "true" "false" "true" "false" ...
 $ agent_01       : chr  "harvest" "none" "development" "none" ...
 $ agent_01_conc  : chr  "harvest_60" "none" "development" "none" ...
 $ ha_affect      : num  3.87 0 1.13 0 0 ...

这是我的查询表:

lookup <- structure(c("harvest_0", "harvest_10", "harvest_20", "harvest_30", 
"harvest_40", "harvest_50", "harvest_60", "harvest_70", "harvest_80", 
"harvest_90", "harvest_00_20", "harvest_00_20", "harvest_00_20", 
"harvest_30_60", "harvest_30_60", "harvest_30_60", "harvest_30_60", 
"harvest_70_90", "harvest_70_90", "harvest_70_90"), .Dim = c(10L, 
2L), .Dimnames = list(NULL, c("list", "val")))

现在我使用查找表查找lookup$list中的任何匹配项,如果找到匹配项,则将其替换为lookup$val中的值。

g <- sapply(df, function(x) { 
  tmp = lookup[, 2][match(x, lookup[, 1])] 
  ifelse(is.na(tmp), x, tmp) 
})

现在我将其强制转换为数据框......

g.df <- as.data.frame(g)

但现在变量的结构都是因素。

str(g.df)
'data.frame':   20 obs. of  5 variables:
 $ year           : Factor w/ 6 levels "2006","2007",..: 3 3 3 5 4 4 6 2 6 4 ...
 $ change_occurred: Factor w/ 2 levels "false","true": 2 1 2 1 1 2 1 1 1 1 ...
 $ agent_01       : Factor w/ 5 levels "agriculture",..: 3 5 2 5 5 1 5 5 5 5 ...
 $ agent_01_conc  : Factor w/ 5 levels "agriculture",..: 3 5 2 5 5 1 5 5 5 5 ...
 $ ha_affect      : Factor w/ 6 levels "0","1.134","1.44",..: 6 1 2 1 1 3 1 1 1 1 ...

有关如何防止这种情况发生的任何想法? -cherrytree

1 个答案:

答案 0 :(得分:1)

我们需要使用lapply代替sapply,因为后者会转换为matrix而矩阵只能容纳一个类。如果有任何字符列,则所有列都将转换为character。当我们使用as.data.frame时,会将其转换为factor,因为默认选项为stringsAsFactors=TRUE

 g <- lapply(df, function(x) { 
    tmp = lookup[, 2][match(x, lookup[, 1])] 
    ifelse(is.na(tmp), x, tmp) 
  })
df2 <- data.frame(g) 
str(df2)
#'data.frame':   20 obs. of  5 variables:
# $ year           : num  2008 2008 2008 2010 2009 ...
# $ change_occurred: Factor w/ 2 levels "false","true": 2 1 2 1 1 2 1 1 1 1 ...
# $ agent_01       : Factor w/ 5 levels "agriculture",..: 3 5 2 5 5 1 5 5 5 5 ...
# $ agent_01_conc  : Factor w/ 5 levels "agriculture",..: 3 5 2 5 5 1 5 5 5 5 ...
# $ ha_affect      : num  3.87 0 1.13 0 0 ...

如果我们真的想使用sapply,则会有一个选项simplify=FALSE,以便它不会强制转换为matrix