循环通过每一列和行,做东西 [英] Loop through each column and row, do stuff

查看:108
本文介绍了循环通过每一列和行,做东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这是描述我想要做的最好的方法:

I think this is the best way to describe what I want to do:

df$column <- ifelse(is.na(df$column) == TRUE, 0, 1)

但是列是动态的。这是因为我有大约45列全部具有相同的内容,所有我想做的是检查每个单元格,如果有一些内容替换为1,如果没有,则替换为1。我当然尝试了许多不同的东西,但是因为R中似乎没有df [index] [column],所以我迷失了。我会期待这样的工作,但不要:

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:

for (index in df) {
  for (column in names(df)) {
    df[[index]][[column]] <- ifelse(is.na(df[[index]][[column]]) == TRUE, 0, 1)
  }
}

我可以用其他语言快速完成(甚至是Excel),但我只是在学习R,并想了解为什么如此简单的东西似乎在使用数据的语言中变得如此复杂。谢谢!

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!

推荐答案

如何做:

df.new = as.data.frame(lapply(df, function(x) ifelse(is.na(x), 0, 1)))

lapply 在数据框的每一列应用一个函数 df 。在这种情况下,该功能将执行0/1替换。 lapply 返回列表。将其包装在 as.data.frame 中将列表转换为数据框(这是一种特殊类型的列表)。

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).

R 中,您经常可以用 * apply 系列函数。在这种情况下, lapply 循环在数据框的列上。此外,许多 R 函数是向量化,意味着函数一次对矢量中的每个值进行操作。在这种情况下, ifelse 在数据框的整个列上进行替换。

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.

这篇关于循环通过每一列和行,做东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆