我正在处理对种族有疑问的调查数据。每个种族类别都是它自己的变量。这是我想做的事情:
- 创建一个新变量,
p.race
。 - 为种族/民族(下)分配
p.race
八个变量之一的值。 - 确定个人是否标记了两个或更多种族,并
p.race
在这种情况下分配值“两个或更多种族”。 - 当他们表示该种族时,分配
p.race
值“西班牙裔或拉丁裔”。 - 创建一个新变量 ,
p.poc
以指示他们是否是有色人种(即,不是白人,包括西班牙裔/拉丁裔)。这应为 0 或 1。
八个种族类别是白人*、黑人*、亚洲*、AIAN*、NHPI*、其他种族*、两个或更多种族*和西班牙裔;其中 * 表示不是西班牙裔或拉丁裔。
到目前为止,这是我尝试解析“两场或多场比赛”的方法:
p['p.race'] <- NA # create new variable for race
# list of variable names that store a string indicating the race
## e.g., `race_white` would be either blank or contain "White, European, Middle Eastern, or Caucasian"
race.list <- c('p.race_white', 'p.race_black', 'p.race_asian', 'p.race_aian', 'p.race_nhpi', 'p.race_other')
# iterate through each record
for ( n in 1:length(p) ) {
multiflag = 0
# iterate through the race list
for ( i in race.list ) {
# if it is not blank, +1 to multiflag
if ( p$i[n] != '' ) {
multiflag <- multiflag + 1
}
}
# if multiflag was flagged more than once, assign "Two or more races" to `race`
if ( multiflag > 1 ) {
p$p.race[n] <- 'Two or more races'
}
}
执行时,它返回此错误:
> Error in if (p$i[n] != "") { : argument is of length zero
这是我的poc
变量编码,错误如下:
p['p.poc'] <- 0 # create a new variable for whether they are a person of color
for ( n in 1:length(p) ) {
if ( p$p.race_black[n] == 'Black, African-American, or African'
| p$p.race_asian[n] == 'Asian or Asian-American'
| p$p.race_aian[n] == 'American Indian or Alaskan Native'
| p$p.race_nhpi[n] == 'Native Hawaiian or other Pacific Islander'
| p$p.race_other[n] == 'Other (please specify)'
| p$p.hispanic[n] == 'Yes') {
p$p.poc[n] <- 1
}
}
> Error in if (p$p.race_black[n] == "Black, African-American, or African" | :
missing value where TRUE/FALSE needed
我真的不知道从哪里开始分配race
八个种族类别之一的新变量,而不使它成为一个很长的代码。
如果有帮助,以下是调查问题:
Q1。您认为自己是西班牙裔、拉丁裔还是西班牙裔?
- 是的
- 不
Q2。您认同哪个种族(勾选所有适用项)?
- 白人、欧洲人、中东人或高加索人
- 黑人、非裔美国人或非洲人
- 亚洲人或亚裔美国人
- 美洲印第安人或阿拉斯加原住民
- 夏威夷原住民或其他太平洋岛民
- 其他(请注明)
这是示例输出(文本被截断):
> p[264:271]
#
# p.hispanic p.race_white p.race_black p.race_asian p.race_aian p.race_nhpi p.race_other
# 1 Yes White
# 2 No White
# 3 No Black
# 4 No White Asian
# 5 Yes Some other race
这是一个dput
输出:
> dput(p[264:270])
structure(list(p.hispanic = structure(c(2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("", "No", "Yes"
), class = "factor"), p.race_white = structure(c(2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L,
2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L), .Label = c("",
"White, European, Middle Eastern, or Caucasian"), class = "factor"),
p.race_black = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("",
"Black, African-American, or African"), class = "factor"),
p.race_asian = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L), .Label = c("",
"Asian or Asian-American"), class = "factor"), p.race_aian = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L,
1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("", "American Indian or Alaskan Native"
), class = "factor"), p.race_nhpi = c(NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA),
p.race_other = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("",
"Other (please specify)"), class = "factor")), .Names = c("p.hispanic",
"p.race_white", "p.race_black", "p.race_asian", "p.race_aian",
"p.race_nhpi", "p.race_other"), class = "data.frame", row.names = c(NA,
-79L))