使用 If else 检查多个列并根据字符串响应的响应创建新列
我有以下数据集:
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(如果不带星号,则为空白)出现在该行中)。它应该看起来像这样:
id | typedad | colordad | typemom | colormom | typekid1 | colorkid1 | Straight | curly | wavy | Mixed |
---|---|---|---|---|---|---|---|---|---|---|
1 | striaght* | Brown | Curly | Blonde | wavy | Black | 2 | 1 | 1 | |
2 | Curly | Black | wavy* | Red | Mixed* | Blonde | 1 | 2 | 2 |
我的两个问题是所有其他示例都使用数字值和所有其他示例的感兴趣的列彼此相邻。我需要看起来匹配可以位于数据帧中任何位置的列中的字符串的代码。我已经尝试过以下操作:
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:
id | typedad | colourdad | typemom | colourmom | typekid1 | colourkid1 | straight | curly | wavy | mixed |
---|---|---|---|---|---|---|---|---|---|---|
1 | striaght* | brown | curly | blonde | wavy | black | 2 | 1 | 1 | |
2 | curly | black | wavy* | red | mixed* | blonde | 1 | 2 | 2 |
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是更有效的答案,也不是更通用的解决方案,但可能满足一个解决方案:
这应该允许您获得预期的结果。或者,您可以使用
assign()
函数,即其中 x 是单词中的每个元素。
所以在一个循环中:
This is not the more efficient answer, neither the more general solution, but may satisfy a solution:
This should allow you to get the expected result. Alternatively, you can use the
assign()
function, i.ewhere x is each element in words.
So in a loop: