Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
203 views
in Technique[技术] by (71.8m points)

r - Issue with using Pipe for multiple ifelse functions

i am using the tidyverse package and working with the pipe term "%>%" how ever after using it on multiple functions i seem to get an error(can not find function ) -.-^and i did not find any info on this on term ("%>%<-") in the net.

 #AB1 
  df_1$AB1<- ifelse(df_1$AB1==1 , 1 ,  
                        ifelse(df_1$AB1== 2, as.integer(-1),
                               ifelse(df_1$AB1== 3, 0,
                                      ifelse(df_1$AB1== 4,0,NA)))) %>%  
#AB2
  df_1$AB2<- ifelse(df_1$AB2==1 , 1 ,  
                     ifelse(df_1$AB2== 2, as.integer(-1),
                            ifelse(df_1$AB2== 3, 0,
                                   ifelse(df_1$AB2== 4,0,NA)))) 

i have tried canging the dataframe to df_2 f.e. or to remove the seconddf_1$AB2<-

#AB1 
  df_1$AB1<- ifelse(df_1$AB1==1 , 1 ,  
                        ifelse(df_1$AB1== 2, as.integer(-1),
                               ifelse(df_1$AB1== 3, 0,
                                      ifelse(df_1$AB1== 4,0,NA)))) %>%  
#AB2
  ifelse(df_1$AB2==1 , 1 ,  
                     ifelse(df_1$AB2== 2, as.integer(-1),
                            ifelse(df_1$AB2== 3, 0,
                                   ifelse(df_1$AB2== 4,0,NA))))

seems like i do not realy get the pipe thing. i wanted to make R run both functions after eaach other but it does not seem wo work? Is there a way to save some time since i would like to avoid going "ctrl" +"r" on many diffrent functions each time i want to change my data in this way.

Bests regards and ty for taking looking at this !

question from:https://stackoverflow.com/questions/66050679/issue-with-using-pipe-for-multiple-ifelse-functions

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should first determine your whole dataframe as data for the pipe. After that, you should not provide the data input for every function, the pipe does that, that is the magic of pipes.

Than you could use transmute to transform your desired columns. case_when() is a nice, cleaner alternative to multiple neste ifelse()s.

Without an example from your data (please post (dput(head(df_1)), and the results of your attempts, it is hard to tell exactly what should work, but you may be ok with this: Try this.


 #AB1 
  df_1 %>% transmute(AB1=case_when(
                        AB1==1 ~ 1 ,  
                        AB1== 2 ~ as.integer(-1),
                        AB1== 3 ~ 0,
                        AB1== 4 ~ 0,
                        TRUE ~ NA) %>%

         transmute(AB2=case_when(
                        AB2==2 ~ 1 ,  
                        AB2== 2 ~ as.integer(-1),
                        AB2== 3 ~ 0,
                        AB2== 4 ~ 0,
                        TRUE ~ NA) %>%


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...