Reputation: 11
for(i in 1:nrow(survey)){
for(j in 1:ncol(survey)){
if(survey[i,j] < 1 | survey[i,j] >10){
survey[i,j] <- "NA"
}
}
}
I have this data set:
survey <- data.frame("q1" = c(5, 3, 2, 7, 11, 5),
"q2" = c(4, 2, 2, 5, 5, 2),
"q3" = c(2, 1, 4, 2, 9, 10),
"q4" = c(2, 5, 2, 5, 4, 2),
"q5" = c(1, 4, -20, 2, 11, 2))
I would like to replace all the numbers smaller than 1 and larger than 10, so I wrote the r code above. And I got this when running the code:
q1 q2 q3 q4 q5
1 5 4 2 2 1
2 3 2 1 5 4
3 2 2 4 2 NA
4 7 5 2 5 NA
5 NA 5 9 4 NA
6 NA 2 10 2 NA
Why it does not work? What do I miss in the code? Could anyone advise me, please?
Upvotes: 0
Views: 353
Reputation: 886938
Another way would be to use the set function of is.na
i.e. is.na<-
is.na(survey) <- survey > 10|survey < 1
survey
q1 q2 q3 q4 q5
1 5 4 2 2 1
2 3 2 1 5 4
3 2 2 4 2 NA
4 7 5 2 5 2
5 NA 5 9 4 NA
6 5 2 10 2 2
The main issue as mentioned in the comments is "NA"
is character string. If we do an assignment to an element in a column that is already a numeric one with character, the whole column is converted to character
. Instead, use NA
for(i in 1:nrow(survey)){
for(j in 1:ncol(survey)){
if(survey[i,j] < 1 | survey[i,j] >10){
survey[i,j] <- NA
}
}
}
survey
q1 q2 q3 q4 q5
1 5 4 2 2 1
2 3 2 1 5 4
3 2 2 4 2 NA
4 7 5 2 5 2
5 NA 5 9 4 NA
6 5 2 10 2 2
The comparison operators works differently for character
values i.e.
> "10" > "1"
[1] TRUE
> "10" > "5"
[1] FALSE
Upvotes: 1
Reputation: 9485
It could be much easier, try something like this:
survey[survey > 10 | survey < 1] <- NA
survey
q1 q2 q3 q4 q5
1 5 4 2 2 1
2 3 2 1 5 4
3 2 2 4 2 NA
4 7 5 2 5 2
5 NA 5 9 4 NA
6 5 2 10 2 2
Upvotes: 1