Loop over every column and row, do something
I think this is the best way to describe what I want to do:
df$column <- ifelse(is.na(df$column) == TRUE, 0, 1)
But where is the column dynamic. This is because I have about 45 columns with the same content and all I want to do is check each cell, replace it with 1 if there is something in it, 0 if not. I've of course tried a lot of different things, but since there doesn't seem to be df [index] [column] in R, I'm lost. I would have expected this to work, but no:
for (index in df) {
for (column in names(df)) {
df[[index]][[column]] <- ifelse(is.na(df[[index]][[column]]) == TRUE, 0, 1)
}
}
I could do it quickly in other languages (or even Excel), but I'm just learning R and want to understand why something so simple seems to be so complicated in a language that is designed to work with data. Thank you!
source to share
How about this:
df.new = as.data.frame(lapply(df, function(x) ifelse(is.na(x), 0, 1)))
lapply
applies a function to each column of the data frame df
. In this case, the function replaces 0/1. lapply
returns a list. The wrapper in as.data.frame
converts the list to a data frame (which is a special type of list).
As R
you can often replace one cycle of families of functions *apply
. In this case, lapply
"loops" through the columns of the data frame. In addition, many functions R
are "vectorized", which means that the function acts on every value in the vector at the same time. In this case ifelse
, replaces the entire column of the data frame.
source to share