我正在尝试使用if条件在R中创建一个新列。
我想创建一个只包含M和F(男性和女性)的列和“na”什么时候是其他的,例如,M?或空白
我的专栏
str(Italy_Jan2018_Climate $ sex)chr [1:3130]“F”“F”“M”“M”“F”“F” “F”“F”“F”“F”“F”“M”“F”“F”“F”“M”“F”“F”“M”“F”“M”“F”“M “ ...
我正在创作的那个
Italy_Jan2018_Climate ["sex_adult"] <- NA
Italy_Jan2018_Climate$sex_adult <-
if(Italy_Jan2018_Climate$sex ==F | Italy_Jan2018_Climate$sex ==M){
sex_adult == Italy_Jan2018_Climate$sex
}else {
sex_adult = na
}
但不知何故不承认我的M
错误:未找到对象'M'
我也试过这个:
> Italy_Jan2018_Climate ["sex_adult"] <- NA
> Italy_Jan2018_Climate$sex_adult <-
+ if(Italy_Jan2018_Climate$sex ==F | Italy_Jan2018_Climate$sex ==M){
+ sex_adult == Italy_Jan2018_Climate$sex
+ }else {
+ sex_adult = na
+ }
Error: object 'M' not found
dput
structure(list(abbpop = c("AL", "AL", "AL", "AL", "AL", "AL"),
label = c("AL03", "AL09", "AL10", "AL13", "AL15", "AL16"),
code = c("AL03", "AL09", "AL10", "AL13", "AL15", "AL16"),
number = c(330, 336, 337, 340, 342, 343), Year = c(2014,
2014, 2014, 2014, 2014, 2014), date = c(41739, 41739, 41739,
41739, 41739, 41739), country = c("ITA", "ITA", "ITA", "ITA",
"ITA", "ITA"), lineage = c(NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_
), pop = c("Cerreto Alpi", "Cerreto Alpi", "Cerreto Alpi",
"Cerreto Alpi", "Cerreto Alpi", "Cerreto Alpi"), sex = c("F",
"F", "M", "M", "F", "F"), svl = c(60, 60, 59, 62, 60, 70),
tot = c(182, 160, 182, 193, 142, 145), mass = c(6.18, 4.6,
5.57, 5.97, 6, 8.41), headlength = c(12.6, 12.4, 14.7, 15.1,
12, 14.2), headwidth = c(6.6, 6, 7.7, 7.2, 6.1, 7.5), Tdorsal = c(1,
1, 1, 1, 1, 1), Gdorsal = c(1, 1, 1, 1, 1, 1), reg = c("0",
"0", "0", "0", "1", "1"), reglength = c(NA, NA, NA, NA, "45",
"67"), cops = c("4", "1", NA, NA, "3", "0"), scars = c("0",
"0", "0", "2", "0", "0"), notes = c("ovulated", "ovulated",
NA, NA, "ovulated", "ovulated, almost egy, slight tendency for red ventral, lost 02/300"
), green = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_), green2 = c(NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_), greenobj = c(NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_
), blackness = c(0.03, 0.18, 0.42, 0.28, 0.07, 0.33), Bssize = c(NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_), biteforce = c(NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_
), testes = c(NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_), Chest = c("W",
"W", "W", "W", "W", "W"), Throat = c("W", "W", "W", "W",
"W", "W"), `Mara green1` = c(NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_), `Mara green2` = c(NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_), Habitat = c(NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_
), phenotype = c(1, 1, 1, 1, 1, 1), group_phenotype = c(1,
1, 1, 1, 1, 1), Latitude = c(44.322488, 44.322488, 44.322488,
44.322488, 44.322488, 44.322488), Longitude = c(10.25085,
10.25085, 10.25085, 10.25085, 10.25085, 10.25085), amt = c(16.9500001271566,
16.9500001271566, 16.9500001271566, 16.9500001271566, 16.9500001271566,
16.9500001271566), mdt = c(1.75000003973643, 1.75000003973643,
1.75000003973643, 1.75000003973643, 1.75000003973643, 1.75000003973643
), iso = c(1751, 1751, 1751, 1751, 1751, 1751), ts = c(233,
233, 233, 233, 233, 233), maxtwarm = c(58, 58, 58, 58, 58,
58), mintcoldm = c(33.4769734501678, 33.4769734501678, 33.4769734501678,
33.4769734501678, 33.4769734501678, 33.4769734501678), tar = c(627,
627, 627, 627, 627, 627), mintwetq = c(263, 263, 263, 263,
263, 263), mtdrq = c(263, 263, 263, 263, 263, 263), mtwarmq = c(263,
263, 263, 263, 263, 263), mtcoldq = c(475, 475, 475, 475,
475, 475), ap = c(8.47499991953373, 8.47499991953373, 8.47499991953373,
8.47499991953373, 8.47499991953373, 8.47499991953373), pwetm = c(33.6309526343735,
33.6309526343735, 33.6309526343735, 33.6309526343735, 33.6309526343735,
33.6309526343735), pdrm = c(622.587926122833, 622.587926122833,
622.587926122833, 622.587926122833, 622.587926122833, 622.587926122833
), ps = c(21.8999996185303, 21.8999996185303, 21.8999996185303,
21.8999996185303, 21.8999996185303, 21.8999996185303), pwetq = c(-3.29999995231628,
-3.29999995231628, -3.29999995231628, -3.29999995231628,
-3.29999995231628, -3.29999995231628), pdrq = c(25.1999995708466,
25.1999995708466, 25.1999995708466, 25.1999995708466, 25.1999995708466,
25.1999995708466), pwarmq = c(5.54999985297521, 5.54999985297521,
5.54999985297521, 5.54999985297521, 5.54999985297521, 5.54999985297521
), pcoldq = c(16.9500001271566, 16.9500001271566, 16.9500001271566,
16.9500001271566, 16.9500001271566, 16.9500001271566), sex_adult = c(NA,
NA, NA, NA, NA, NA)), .Names = c("abbpop", "label", "code",
"number", "Year", "date", "country", "lineage", "pop", "sex",
"svl", "tot", "mass", "headlength", "headwidth", "Tdorsal", "Gdorsal",
"reg", "reglength", "cops", "scars", "notes", "green", "green2",
"greenobj", "blackness", "Bssize", "biteforce", "testes", "Chest",
"Throat", "Mara green1", "Mara green2", "Habitat", "phenotype",
"group_phenotype", "Latitude", "Longitude", "amt", "mdt", "iso",
"ts", "maxtwarm", "mintcoldm", "tar", "mintwetq", "mtdrq", "mtwarmq",
"mtcoldq", "ap", "pwetm", "pdrm", "ps", "pwetq", "pdrq", "pwarmq",
"pcoldq", "sex_adult"), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
提前致谢, 马拉
答案 0 :(得分:1)
我看到多个语法错误。您应该能够使用以下单行进行此更新:
Italy_Jan2018_Climate$sex_adult <-
ifelse(Italy_Jan2018_Climate$sex %in% c('M', 'F'),
Italy_Jan2018_Climate$sex,
NA)