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 column is dynamic. This is because I have about 45 columns all with the same kind of content, and all I want to do is check each cell, replace it with a 1 if there's something in it, a 0 if not. I have of course tried many different things, but since there seems to be no df[index][column] in R, I'm lost. I'd have expected something like this to work, but nope:
但是列是动态的。这是因为我有大约45列都具有相同类型的内容,我想要做的就是检查每个单元格,如果有内容则替换为1,否则为0。我当然尝试了很多不同的东西,但由于R中似乎没有df [index] [column],我迷路了。我曾经期望这样的事情有效,但是没有:
for (index in df) {
for (column in names(df)) {
df[[index]][[column]] <- ifelse(is.na(df[[index]][[column]]) == TRUE, 0, 1)
}
}
I could do this 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's meant to work with data. Thanks!
我可以用其他语言(甚至是Excel)快速完成这项工作,但我只是在学习R并且想要理解为什么这么简单的事情似乎在一种用于处理数据的语言中如此复杂。谢谢!
1 个解决方案
#1
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 does the 0/1 replacement. lapply
returns a list. Wrapping it in as.data.frame
converts the list to a data frame (which is a special type of list).
lapply将函数应用于数据帧df的每一列。在这种情况下,该功能进行0/1替换。 lapply返回一个列表。将其包装在as.data.frame中会将列表转换为数据框(这是一种特殊类型的列表)。
In R
you can often replace a loop with one of the *apply
family of functions. In this case, lapply
"loops" over the columns of the data frame. Also, many R
functions are "vectorized" meaning the function operates on every value in a vector at once. In this case, ifelse
does the replacement on an entire column of the data frame.
在R中,您通常可以使用* apply系列函数替换循环。在这种情况下,lapply在数据帧的列上“循环”。此外,许多R函数是“矢量化”的,这意味着函数一次对矢量中的每个值进行操作。在这种情况下,ifelse在数据帧的整个列上进行替换。
#1
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 does the 0/1 replacement. lapply
returns a list. Wrapping it in as.data.frame
converts the list to a data frame (which is a special type of list).
lapply将函数应用于数据帧df的每一列。在这种情况下,该功能进行0/1替换。 lapply返回一个列表。将其包装在as.data.frame中会将列表转换为数据框(这是一种特殊类型的列表)。
In R
you can often replace a loop with one of the *apply
family of functions. In this case, lapply
"loops" over the columns of the data frame. Also, many R
functions are "vectorized" meaning the function operates on every value in a vector at once. In this case, ifelse
does the replacement on an entire column of the data frame.
在R中,您通常可以使用* apply系列函数替换循环。在这种情况下,lapply在数据帧的列上“循环”。此外,许多R函数是“矢量化”的,这意味着函数一次对矢量中的每个值进行操作。在这种情况下,ifelse在数据帧的整个列上进行替换。