使用for循环向现有数据框添加新变量。

huangapple go评论63阅读模式
英文:

adding new variables to a existing dataframe using for loop

问题

我有一个包括以下变量的数据集:
ada_27、ada_28、ada_29、ada_30,...,ada_38
每个变量的值可能是0、1或NA。

我想使用以下代码创建新变量ada_27_p、ada_28_p,...,ada_38_p(指示变量,表示每个变量是否包含值0或NA):

dat$ada_@@_p <- ifelse((dat$ada_@@==0 | is.na(dat$ada_@@)),1,0)
# @@ 是数字27到38

我需要帮助将这个过程放入for循环函数中,这样我就不需要在R中编写重复的代码。谢谢!

我在将上述函数放入for循环中时遇到了问题,因为我不知道如何在循环中将新创建的变量添加到数据框中。

英文:

I have a dataset which include the following variables:
ada_27, ada_28, ada_29, ada_30,....., ada_38
Each variable may have a value of 0, 1, or NA.

I want to create new variables ada_27_p, ada_28_p,..., ada_38_p (indicator variable whether each variable contains value of 0 or NA) using the following code:

dat$ada_@@_p &lt;- ifelse((dat$ada_@@==0 | is.na(dat$ada_@@)),1,0)
# @@ is numbers 27 to 38

I need help with puttin this procedure in for loop function so that I do not need to write repetitive codes in R. Thanks!

I have trouble putting the above function in for loop as I do not know how to add newly created variables to the dataframe in the loop.

答案1

得分: 0

请提供下次的一些数据。我创建了你的数据的一个简单版本。

你要求一个for循环。横跨列的for循环可能会有问题。由于你试图横跨列进行操作,使用across()更容易执行所需的操作。

library(tidyverse)

# 你的数据框的简化版本
df <- data.frame(ada_1 = sample(v, 10, T), ada_2 = sample(v, 10, T), ada_3 = sample(v , 10, T), ada_4 = sample(v, 10, T))

# 它是什么样子
df
   ada_1 ada_2 ada_3 ada_4
1      1     0     1     0
2     NA     1     0    NA
3      0    NA    NA     1
4      0     0    NA     1
5      1    NA     0     1
6      1    NA     0     1
7      1     1     1     0
8      1    NA     1    NA
9     NA     1    NA    NA
10    NA    NA    NA    NA

# mutate() 在数据框上执行操作。across在列之间工作。
# 阅读文档以获取更多细节。
df <- df %>% mutate(across( ada_1:ada_4, ~ ifelse(. == 0 | is.na(.), 1, 0), .names="{.col}_p") )

df
   ada_1 ada_2 ada_3 ada_4 ada_1_p ada_2_p ada_3_p ada_4_p
1      1     0     1     0       0       1       0       1
2     NA     1     0    NA       1       0       1       1
3      0    NA    NA     1       1       1       1       0
4      0     0    NA     1       1       1       1       0
5      1    NA     0     1       0       1       1       0
6      1    NA     0     1       0       1       1       0
7      1     1     1     0       0       0       0       1
8      1    NA     1    NA       0       1       0       1
9     NA     1    NA    NA       1       0       1       1
10    NA    NA    NA    NA       1       1       1       1

这是你提供的代码的翻译。

英文:

Please provide some data next time. I created a simple version of your data.

You asked for a for loop. For loops across columns can be problematic. Since you're trying to work across columns, it's even easier to perform your desired operations across the columns with across().

library(tidyverse)
# Small version of your dataframe
df &lt;- data.frame(ada_1 = sample(v, 10, T), ada_2 = sample(v, 10, T), ada_3 = sample(v , 10, T), ada_4 = sample(v, 10, T))
# What it looks like
df
ada_1 ada_2 ada_3 ada_4
1      1     0     1     0
2     NA     1     0    NA
3      0    NA    NA     1
4      0     0    NA     1
5      1    NA     0     1
6      1    NA     0     1
7      1     1     1     0
8      1    NA     1    NA
9     NA     1    NA    NA
10    NA    NA    NA    NA
# mutate() performs operations on data frame. across works across columns.
# Read the documentation for more details.
df &lt;- df %&gt;% mutate(across( ada_1:ada_4, ~ ifelse(. == 0 | is.na(.), 1, 0), .names=&quot;{.col}_p&quot;) )
df
ada_1 ada_2 ada_3 ada_4 ada_1_p ada_2_p ada_3_p ada_4_p
1      1     0     1     0       0       1       0       1
2     NA     1     0    NA       1       0       1       1
3      0    NA    NA     1       1       1       1       0
4      0     0    NA     1       1       1       1       0
5      1    NA     0     1       0       1       1       0
6      1    NA     0     1       0       1       1       0
7      1     1     1     0       0       0       0       1
8      1    NA     1    NA       0       1       0       1
9     NA     1    NA    NA       1       0       1       1
10    NA    NA    NA    NA       1       1       1       1

huangapple
  • 本文由 发表于 2023年2月24日 01:24:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548258.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定