R:更改多列以创建新列 [英] R: mutate over multiple columns to create a new column

查看:57
本文介绍了R:更改多列以创建新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据其他列的值创建一个新列.

I'm trying to create a new column based on values of other columns.

这里是一个与我正在研究的数据集相似的数据集.我有三个变量,Test1,Test2,Test3.我正在尝试创建一个新的变量,如果任何测试变量为1DF60,DF61,DF63或DF64,否则为0.

Here is a dataset that is similar to what I'm working on. I have three variables, Test1, Test2, Test3. I am trying to create a new variable that is 1 if any of the Test variables are either DF60, DF61, DF63 or DF64 or 0 otherwise.

Test1<-c("DF64", "DF63", "DF89", "DF30", "DF70")
Test2<-c("DF61", "DF25", "DF00", "DF30", "DF99")
Test3<-c("DF80", "DF63", "DF60", "DF63", "DF70")
Test<-data.frame(Test1, Test2, Test3)

我目前有很多 ifelse 语句,正在寻找可以循环遍历多个Test变量,同时允许查找多个值.

I have many ifelse statements at the moment and am looking for a code that can loop over the multiple Test variables while allowing multiple values to be looked up.

在浏览了一些类似的线程之后,我尝试了mutate_at,但是我认为这不是正确的方法.

After looking through some similar threads, I tried mutate_at but I don't think that is the right way.

Test2<- Test %>%
mutate_at(vars(starts_with("Test")), funs(Test=ifelse("DF60" | "DF61" | 
"DF62" | "DF63", 1, 0)))

非常感谢您的帮助!

谢谢!

推荐答案

Test2<- Test %>%
dplyr::select(starts_with("Test"))%>%
mutate_all(function(x){x %in% c("DF60","DF61","DF62","DF63")})%>%
mutate(out = ifelse(rowSums(.)<1,0,1))

评论后调整

如果要保留其他列,则由yutannihilation提出的mutate_at要好得多.然后,问题就变成了在选择列时对rowums进行突变.不知道下一件事是否是最佳实践,但它能奏效(对我之前的问题进行了重新解答:

If you want to keep other columns, mutate_at, as is proposed by yutannihilation, is far better. The problem then becomes doing the rowsums in mutate on a selection of the columns. No idea if the next thing is best practice, but it works (reworked an answer on a previous question of mine: dplyr mutate on column subset (one function on all these columns combined))

library(tidyverse)
library(anomalyDetection)

Test1<-c("DF64", "DF63", "DF89", "DF30", "DF70")
Test2<-c("DF61", "DF25", "DF00", "DF30", "DF99")
Test3<-c("DF80", "DF63", "DF60", "DF63", "DF70")
Test<-data.frame(Test1, Test2, Test3)

Test$ExtraCol<-LETTERS[1:5]


Test2<- Test %>%
  mutate_at(vars(starts_with("Test")),funs(bin=.%in% c("DF60","DF61","DF62","DF63")))%>%
  split(.,1<10)%>%
  map_df(~mutate(.,out=rowSums(.[paste0("Test",1:3,"_bin")])>0))


  Test1 Test2 Test3 ExtraCol Test1_bin Test2_bin Test3_bin   out
   DF64  DF61  DF80        A     FALSE      TRUE     FALSE  TRUE
   DF63  DF25  DF63        B      TRUE     FALSE      TRUE  TRUE
   DF89  DF00  DF60        C     FALSE     FALSE      TRUE  TRUE
   DF30  DF30  DF63        D     FALSE     FALSE      TRUE  TRUE
   DF70  DF99  DF70        E     FALSE     FALSE     FALSE FALSE

这篇关于R:更改多列以创建新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆