Stata的foreach命令在R中的替代方法是什么?

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

Alternative of Stata's foreach command in R

问题

我是R的新用户,习惯使用Stata软件。

我以前在Stata中使用foreach命令循环遍历多个变量。例如,我可以将多个数值变量转换为因子变量。

在Stata中,首先定义标签:

label define NoYes 0 "No" 1 "Yes"

然后,应用循环命令:

for (x in c("var1", "var2", "var3")) {
  levels(x) <- c("No", "Yes")
}

我正在努力弄清楚如何在R中完成这个操作,任何帮助将不胜感激。

英文:

I am a new user of R and am used to using Stata software.

I used to loop through multiple variables by foreach command in Stata. So, for example, I can convert multiple numerical variables to factor ones.

In Stata, first, define the label:

label define NoYes 0 &quot;No&quot; 1 &quot;Yes&quot;

Then, apply the loop command:

foreach x in var1 var2 var3 {
	label values `x&#39; NoYes
}

I am figuring out how I can do so in R; any help would be appreciated.

答案1

得分: 1

Example data:

library(dplyr)
df <- data.frame(var1 = c(1, 1, 0, 0), 
         var2 = c(0, 0, 1, 1), 
         var3 = c(0, 1, 0, 1)) 

There may be several alternatives, and across of dplyr is one of them.

new_df <- df %>%
  mutate(across(var1:var3, ~ factor(.x, levels = c(0, 1), labels=c("No", "Yes"))))

new_df
  var1 var2 var3
1  Yes   No   No
2  Yes   No  Yes
3   No  Yes   No
4   No  Yes  Yes

You do not really need levels = c(0, 1) here, but I would always do it in real data just to be safe.

英文:

Example data:

library(dplyr)
df &lt;- data.frame(var1 = c(1, 1, 0, 0), 
         var2 = c(0, 0, 1, 1), 
         var3 = c(0, 1, 0, 1)) 

There may be several alternatives, and across of dplyr is one of them.

new_df &lt;- df %&gt;% mutate(across(var1:var3, ~ factor(.x, levels = c(0, 1), labels=c(&quot;No&quot;, &quot;Yes&quot;))))

new_df
  var1 var2 var3
1  Yes   No   No
2  Yes   No  Yes
3   No  Yes   No
4   No  Yes  Yes

You do not really need levels = c(0, 1) here, but I would always do it in real data just to be safe.

答案2

得分: 1

In base R, we can use lapply.

dat[c(1, 3)] <- lapply(dat[c(1, 3)], factor, levels = 0:1, labels = c('No', 'Yes'))
dat
#    X1 X2  X3 X4 X5
# 1  No  1  No  1  0
# 2  No  1 Yes  1  1
# 3  No  0  No  0  0
# 4  No  1  No  0  0
# 5 Yes  0 Yes  0  0
# 6 Yes  1 Yes  0  0

To avoid confusion, I generally recommend not using too many fancy packages while you're new to R.

The literal translation could look like this (reload dat before trying):

for (x in c('X1', 'X3')) {
  dat[[x]] <- factor(dat[[x]], levels = 0:1, labels = c('No', 'Yes'))
}
dat
#    X1 X2  X3 X4 X5
# 1  No  1  No  1  0
# 2  No  1 Yes  1  1
# 3  No  0  No  0  0
# 4  No  1  No  0  0
# 5 Yes  0 Yes  0  0
# 6 Yes  1 Yes  0  0

Data:

dat <- structure(list(X1 = c(0L, 0L, 0L, 0L, 1L, 1L), X2 = c(1L, 1L, 
0L, 1L, 0L, 1L), X3 = c(0L, 1L, 0L, 0L, 1L, 1L), X4 = c(1L, 1L, 
0L, 0L, 0L, 0L), X5 = c(0L, 1L, 0L, 0L, 0L, 0L)), class = "data.frame", row.names = c(NA, 
-6L))
英文:

In base R we can use lapply.

dat[c(1, 3)] &lt;- lapply(dat[c(1, 3)], factor, levels=0:1, labels=c(&#39;No&#39;, &#39;Yes&#39;))
dat
#    X1 X2  X3 X4 X5
# 1  No  1  No  1  0
# 2  No  1 Yes  1  1
# 3  No  0  No  0  0
# 4  No  1  No  0  0
# 5 Yes  0 Yes  0  0
# 6 Yes  1 Yes  0  0

To avoid confusion, I generally recommend not using too many fancy packages while you're new to R.

The literal translation could look like this (reload dat before trying):

for (x in c(&#39;X1&#39;, &#39;X3&#39;)) {
  dat[[x]] &lt;- factor(dat[[x]], levels=0:1, labels=c(&#39;No&#39;, &#39;Yes&#39;))
}
dat
#    X1 X2  X3 X4 X5
# 1  No  1  No  1  0
# 2  No  1 Yes  1  1
# 3  No  0  No  0  0
# 4  No  1  No  0  0
# 5 Yes  0 Yes  0  0
# 6 Yes  1 Yes  0  0

Data:

dat &lt;- structure(list(X1 = c(0L, 0L, 0L, 0L, 1L, 1L), X2 = c(1L, 1L, 
0L, 1L, 0L, 1L), X3 = c(0L, 1L, 0L, 0L, 1L, 1L), X4 = c(1L, 1L, 
0L, 0L, 0L, 0L), X5 = c(0L, 1L, 0L, 0L, 0L, 0L)), class = &quot;data.frame&quot;, row.names = c(NA, 
-6L))

huangapple
  • 本文由 发表于 2023年2月19日 03:15:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75495788.html
匿名

发表评论

匿名网友

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

确定