2017-03-12 36 views
0

我有一個數據幀(稱爲hp),其中包含更多與NA-s的列。這些列的類是因子。首先,我想將其改爲人物,用「無」填充NA-s並將其改回因子。我有14列,因爲它我想用循環。但它不起作用。在數據幀中更改NA-s更多列

Thx尋求幫助。

列:

miss_names<-c("Alley","MasVnrType","FireplaceQu","PoolQC","Fence","MiscFeature","GarageFinish",  "GarageQual","GarageCond","BsmtQual","BsmtCond","BsmtExposure","BsmtFinType1", 
      "BsmtFinType2","Electrical") 

循環:

for (i in miss_names){  
    hp[i]<-as.character(hp[i]) 
    hp[i][is.na(hp[i])]<-"NONE" 
    hp[i]<-as.factor(hp[i]) 
    print(hp[i]) 
    } 

Error in sort.list(y) : 'x' must be atomic for 'sort.list' 
Have you called 'sort' on a list? 
+0

請提供一個可重現的例子。添加幾行hp對象,理想情況下使用'dput'。 http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example –

回答

1

使用addNA()添加NA的一個因素的水平,然後替換爲任何你想要的那個水平。您不必首先將這些因素轉換爲角色矢量。您可以遍歷數據框架中的所有因素並逐個替換它們。

# Sample data 
dd <- data.frame(
    x = sample(c(NA, letters[1:3]), 20, replace = TRUE), 
    y = sample(c(NA, LETTERS[1:3]), 20, replace = TRUE) 
) 

# Loop over the columns 
for (i in seq_along(dd)) { 
    xx <- addNA(dd[, i]) 
    levels(xx) <- c(levels(dd[, i]), "none") 
    dd[, i] <- xx 
} 

這給了我們

> str(dd) 
'data.frame': 20 obs. of 2 variables: 
$ x: Factor w/ 4 levels "a","b","c","none": 1 4 1 4 4 1 4 3 3 3 ... 
$ y: Factor w/ 4 levels "A","B","C","none": 1 1 2 2 1 3 3 3 4 1 ... 
+0

感謝您的解決方案。 – deaux

0

使用purrr庫使用相同的數據@約翰·拉鬆另一種解決方案:

library(purrr) 
 

 
set.seed(15) 
 
dd <- data.frame(
 
     x = sample(c(NA, letters[1:3]), 20, replace = TRUE), 
 
     y = sample(c(NA, LETTERS[1:3]), 20, replace = TRUE)) 
 

 
# Create a function to convert NA to none 
 
convert.to.none <- function(x){ 
 
     y <- addNA(x) 
 
     levels(y) <- c(levels(x), "none") 
 
     x <- y 
 
     return(x) } 
 

 
# use the map function to cycle through dd's columns 
 
map_df(dd, convert.2.none)

允許對縮放你的工作。