Reputation: 452
I am trying to write a function to replace the NA values in the second column of a data frame to 0. I know if "x" is my data frame then
x[,2][is.na(x[,2]) <- 0
will do the job. However, I wanted to write a function for this so I can apply to different data frames. I came up with
SetNaToZero <- function(x) {
x <-data.frame(x)
x[1:nrow(x),2][is.na(x[1:nrow(x),2])] <-0
return(x)
}
But it returns the following error when I apply it on a data frame:
Error in `[<-.data.frame`(`*tmp*`, 1:nrow(x), 2, value = numeric(0)) :
replacement has length zero
I appreciate any suggestions.
Upvotes: 1
Views: 382
Reputation: 81683
You can simplify the function:
SetNaToZero <- function(x) {
x[is.na(x[[2]]), 2] <- 0
x
}
Simplify it further:
SetNaToZero <- function(x)
"[<-"(x, is.na(x[[2]]), 2, 0)
Upvotes: 0
Reputation: 9582
I think you are making it harder than you need to. The code you have in the first chunk there would be fine as a function:
SetNaToZero <- function(x) {
x[,2][is.na(x[, 2])] <- 0
return(x)
}
In action:
set.seed(123)
dat <- data.frame(a=rnorm(10),
b=sample(c(NA, 1:3), 10, replace=T))
SetNaToZero(dat)
a b
1 -0.56047565 3
2 -0.23017749 2
3 1.55870831 2
4 0.07050839 3
5 0.12928774 2
6 1.71506499 2
7 0.46091621 2
8 -1.26506123 2
9 -0.68685285 1
10 -0.44566197 0
And if you want to modify the object, you can always do dat <- SetNaToZero(dat)
Upvotes: 2