英文:
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 "No" 1 "Yes"
Then, apply the loop command:
foreach x in var1 var2 var3 {
label values `x' 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 <- 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.
答案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)] <- 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))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论