2017-04-21 59 views
0

內更換因子變量我有dataframes結構的以下列表:不能在列表[R

str(mylist) 

List of 2 
$ L1 :'data.frame': 12471 obs. of 3 variables: 
...$ colA : Date[1:12471], format: "2006-10-10" "2010-06-21" ... 
...$ colB : int [1:12471], 62 42 55 12 78 ... 
...$ colC : Factor w/ 3 levels "type1","type2","type3",..: 1 2 3 2 2 ... 

我想用一個新的因素type4更換type1type2

我曾嘗試:

mylist <- lapply(mylist, transform, colC = 
         replace(colC, colC == 'type1','type4')) 
Warning message: 
1: In `[<-.factor`(`*tmp*`, list, value = "type4") : 
    invalid factor level, NA generated 
2: In `[<-.factor`(`*tmp*`, list, value = "type4") : 
    invalid factor level, NA generated 

我不希望在我的stringAsFactor=F初始數據讀,但我已經嘗試添加type4在我最初的數據集的水平使用(前分裂成dataframes名單) :

levels(mydf$colC) <- c(levels(mydf$colC), "type4") 

但嘗試替換時仍然出現相同的錯誤。

我該如何告訴取代type4是否被視爲一個因素?

回答

0

您可以嘗試使用levels選項來更新您的因子。 如, status <- factor(status, order=TRUE, levels=c("1", "3", "2",...)) c("1", "3", "2",...)是你的type4在這裏。

0

正如您所述,關鍵的是添加新的因子水平。

## Test data: 
mydf <- data.frame(colC = factor(c("type1", "type2", "type3", "type2", "type2"))) 
mylist <- list(mydf, mydf) 

你的數據有三個因子水平:

> str(mylist) 
List of 2 
$ :'data.frame': 5 obs. of 1 variable: 
    ..$ colC: Factor w/ 3 levels "type1","type2",..: 1 2 3 2 2 
$ :'data.frame': 5 obs. of 1 variable: 
    ..$ colC: Factor w/ 3 levels "type1","type2",..: 1 2 3 2 2 

現在添加第四個因素的水平,那麼你的replace命令應該工作:

## Change levels: 
for (ii in seq(along = mylist)) levels(mylist[[ii]]$colC) <- 
    c(levels(mylist[[ii]]$colC), "type4") 

## Replace level: 
mylist <- lapply(mylist, transform, colC = replace(colC, 
    colC == 'type1','type4')) 

新的數據有四個因子水平:

> str(mylist) 
List of 2 
$ :'data.frame': 5 obs. of 1 variable: 
    ..$ colC: Factor w/ 4 levels "type1","type2",..: 4 2 3 2 2 
$ :'data.frame': 5 obs. of 1 variable: 
    ..$ colC: Factor w/ 4 levels "type1","type2",..: 4 2 3 2 2