使用 If else 检查多个列并根据字符串响应的响应创建新列

发布于 2025-01-09 04:44:46 字数 2196 浏览 0 评论 0原文

我有以下数据集:

hairdf=data.frame(
id=c(1:4),
  typedad=c("straight*","curly"),
  colourdad=c("brown","black"),
  typemom=c("curly","wavy*"),
  colourmom=c("blonde","red"),
  typekid1=c("wavy","mixed*"),
  colourkid1=c("black","blonde"))

我想创建新的列来查看发型,如果头发类型出现在不带星号的“发型”列中,则给出值 1;如果带有星号,则给出值 2(如果不带星号,则为空白)出现在该行中)。它应该看起来像这样:

idtypedadcolordadtypemomcolormomtypekid1colorkid1StraightcurlywavyMixed
1striaght*BrownCurlyBlondewavyBlack211
2CurlyBlackwavy*RedMixed*Blonde122

我的两个问题是所有其他示例都使用数字值和所有其他示例的感兴趣的列彼此相邻。我需要看起来匹配可以位于数据帧中任何位置的列中的字符串的代码。我已经尝试过以下操作:

straight<- hairdf %>% mutate(across(c("hairtypedad", "hairtypemom", "hairtypekid1"),
                                    ifelse(.=="straight", 1
                                             ifelse(.=="straight*",2, ""
                                             ))))
curly<- hairdf %>% mutate(across(c("hairtypedad", "hairtypemom", "hairtypekid1"),
                                        ifelse(.=="curly", 1
                                                 ifelse(.=="curly*",2, ""
 wavy<- hairdf %>% mutate(across(c("hairtypedad", "hairtypemom", "hairtypekid1"),
                                        ifelse(.=="wavy", 1
                                                 ifelse(.=="wavy*",2, ""
                                                 ))))      
mixed<- hairdf %>% mutate(across(c("hairtypedad", "hairtypemom", "hairtypekid1"),
                                        ifelse(.=="mixed", 1
                                                 ifelse(.=="mixed*",2, ""
                                                 )))) 

但我不确定这段代码是否有意义。另外,这会很乏味,因为我有更多的发型,所以任何使它更容易的建议也将不胜感激!谢谢你!!!

I have the following dataset:

hairdf=data.frame(
id=c(1:4),
  typedad=c("straight*","curly"),
  colourdad=c("brown","black"),
  typemom=c("curly","wavy*"),
  colourmom=c("blonde","red"),
  typekid1=c("wavy","mixed*"),
  colourkid1=c("black","blonde"))

I want to create new columns that will look at hairtypes and give value 1 if the type of hair appears in "hairtype" columns without an asterisk and a value 2 if it appears with an asterisk (blank if it doesnt appear in that row). It should look like so:

idtypedadcolourdadtypemomcolourmomtypekid1colourkid1straightcurlywavymixed
1striaght*browncurlyblondewavyblack211
2curlyblackwavy*redmixed*blonde122

My two issues are that all other examples use numeric values and all other examples have the columns of interest located next to each other. I need code that looks to match strings in columns that can be located anywhere in the dataframe. I have tried the following:

straight<- hairdf %>% mutate(across(c("hairtypedad", "hairtypemom", "hairtypekid1"),
                                    ifelse(.=="straight", 1
                                             ifelse(.=="straight*",2, ""
                                             ))))
curly<- hairdf %>% mutate(across(c("hairtypedad", "hairtypemom", "hairtypekid1"),
                                        ifelse(.=="curly", 1
                                                 ifelse(.=="curly*",2, ""
 wavy<- hairdf %>% mutate(across(c("hairtypedad", "hairtypemom", "hairtypekid1"),
                                        ifelse(.=="wavy", 1
                                                 ifelse(.=="wavy*",2, ""
                                                 ))))      
mixed<- hairdf %>% mutate(across(c("hairtypedad", "hairtypemom", "hairtypekid1"),
                                        ifelse(.=="mixed", 1
                                                 ifelse(.=="mixed*",2, ""
                                                 )))) 

But I'm not sure if this code even makes sense. Also, this will be tedious as I have way more hairtypes, so any suggestions to make it easier would be appreciated as well!! Thankyou!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤者何惧 2025-01-16 04:44:46

这不是更有效的答案,也不是更通用的解决方案,但可能满足一个解决方案:

#create columns
st <- rep(NA,nrow(hairdf));
cur <- rep(NA,nrow(hairdf));
wav <- rep(NA,nrow(hairdf));
mix <- rep(NA,nrow(hairdf));

#join and define words
hairdf <- cbind(hairdf,st,cur,wav,mix);
words <- c("straight","curly","wavy","mixed");
words_ast <- paste(words,"*",sep=""); #just get the "*" words

#make a loop according to positions of columns st,cur,wav,mix
for (j in 1:length(words_ast)){ #let's see if we can evaluate 2 in words_ast
  for (i in c(2,3,4)){ #but only in columns we selected
    a <- subset(hairdf,hairdf[,i]==words_ast[j]) #subset columns which satisfay condition. [Note that this can be written as hairdf %>% subset(.[,i]==words_ast[j]) ]
    hairdf[row.names(a),7+j] <- 2 #replace value from column 8
  }
}
#repeat process for "words"

for (j in 1:length(words)){
  for (i in c(2,3,4)){
    a <- subset(hairdf,hairdf[,i]==words[j])
    hairdf[row.names(a),7+j] <- 1
  }
}

这应该允许您获得预期的结果。或者,您可以使用 assign() 函数,即

assign(x,value=1)

其中 x 是单词中的每个元素。

所以在一个循环中:

assign(words[n],value=1) ; assign(words_ast[n],value=2)

This is not the more efficient answer, neither the more general solution, but may satisfy a solution:

#create columns
st <- rep(NA,nrow(hairdf));
cur <- rep(NA,nrow(hairdf));
wav <- rep(NA,nrow(hairdf));
mix <- rep(NA,nrow(hairdf));

#join and define words
hairdf <- cbind(hairdf,st,cur,wav,mix);
words <- c("straight","curly","wavy","mixed");
words_ast <- paste(words,"*",sep=""); #just get the "*" words

#make a loop according to positions of columns st,cur,wav,mix
for (j in 1:length(words_ast)){ #let's see if we can evaluate 2 in words_ast
  for (i in c(2,3,4)){ #but only in columns we selected
    a <- subset(hairdf,hairdf[,i]==words_ast[j]) #subset columns which satisfay condition. [Note that this can be written as hairdf %>% subset(.[,i]==words_ast[j]) ]
    hairdf[row.names(a),7+j] <- 2 #replace value from column 8
  }
}
#repeat process for "words"

for (j in 1:length(words)){
  for (i in c(2,3,4)){
    a <- subset(hairdf,hairdf[,i]==words[j])
    hairdf[row.names(a),7+j] <- 1
  }
}

This should allow you to get the expected result. Alternatively, you can use the assign() function, i.e

assign(x,value=1)

where x is each element in words.

So in a loop:

assign(words[n],value=1) ; assign(words_ast[n],value=2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文