简体   繁体   中英

R - Remove values from different columns in a data frame

I have a dataset that contains in some columns two values that I have to change to NA.

'#DIV/0' and '' (nothing)

I solved this problem using a 'for' loop but I would like to know if there is another way, like using 'apply' and what is the faster method.

My code:

train <- read.csv('https://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv',stringsAsFactors = F)
test <- read.csv('https://d396qusza40orc.cloudfront.net/predmachlearn/pml-testing.csv', stringsAsFactors = F)

train2 <- train
for(x in 1:length(train2)){
        train2[train2[,x] %in% c('','#DIV/0'),x] <- NA
}

test2 <- test
for(x in 1:length(test2)){
        test2[test2[,x] %in% c('','#DIV/0'),x] <- NA
}

We can use na.strings argument in the read.csv

train <- read.csv('https://d396qusza40orc.cloudfront.net/predmachlearn/pml-training.csv', 
              na.strings=c('#DIV/0', '', 'NA') ,stringsAsFactors = F)
test <- read.csv('https://d396qusza40orc.cloudfront.net/predmachlearn/pml-testing.csv',
                na.strings= c('#DIV/0', '', 'NA'),stringsAsFactors = F)

Just checking

sum(train=='#DIV/0', na.rm=TRUE)
#[1] 0
sum(test=='#DIV/0', na.rm=TRUE)
#[1] 0
sum(test=='', na.rm=TRUE)
#[1] 0
sum(train=='', na.rm=TRUE)
#[1] 0

The NA values

sum(is.na(train))
#[1] 1921600
sum(is.na(test))
#[1] 2000

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM