trying to mutate 2 possiblilities each of 2 conditions into a new column

I have no clue how to use reprex. I just tried to render in my rstudio and i did. I have no idea how to bring it in to this topic. I am trying my best. so I copied my code and explained what I am trying to do below. I really don't know why the directions for reprex arent more clear for first time attempt. there is a screen shot of my data at bottom.
I am attempting to create a results column for 2 different events each event has 2 requirements. I apologize for insufficiencies. if you could also help me w reprex... I need detailed step by step unfortunately
""
jorge <- alfaro
jorge <- jorge[ , c("fielder_2" , "player_name" , "pitch_type" , "Called_Strike" , "Ball" , "In_zone" , "Out_zone" ,
"plate_x" ,"plate_z")]
sampleRows <- sample(1:nrow(jorge) , 20)
jorge <- jorge[sampleRows , ]

I am trying to get a column named "results" where a pitch that is both called_strike and Out_zone is labled stolenK and

a pitch that is both called a ball and in the zone is labeled LostK.

jorge %>%
mutate(result = ifelse(c(Called_Strike == 1 & Out_zone == 1), "stolenK" , 0))-> jorge

this first mutation worked and I have recieve "stolenK" when the requirements are met (although my sample doesn't contain any)

jorge %>%
mutate(result = ifelse(c(Ball == 1 & In_zone == 1) , "lostK")) -> jorge

this is what the other would be

jorge %>%
mutate(result = ifelse(c(Called_Strike == 1 & Out_zone == 1 | Ball == 1 & In_zone == 1),
"stolenK" | "lostK"))-> jorge

I tried this and recieved the error below

Error in "stolenK" | "lostK" :
operations are possible only for numeric, logical or complex types

ifelse requires a test, a positive result, and a negative result, right? That's what you did with

mutate(result = ifelse(c(Called_Strike == 1 & Out_zone == 1), "stolenK" , 0))-> jorge

So, if the test is met in

mutate(result = ifelse(c(Called_Strike == 1 & Out_zone == 1 | Ball == 1 & In_zone == 1),
"stolenK" | "lostK"))-> jorge
  1. how does mutate decide between stolkenK and lostK
  2. there's no result for when the test fails

So, I tried this and got this error: (attempt to give result for when test fails)

jorge %>%

mutate(result = ifelse(c(Called_Strike == 1 & Out_zone == 1 | Ball == 1 & In_zone == 1),

                     "stolenK", "lostK" , 0))-> jorge

Error in ifelse(c(Called_Strike == 1 & Out_zone == 1 | Ball == 1 & In_zone == :

                unused argument (0)

And this and received this error:

jorge %>%

mutate(result = ifelse(c(Called_Strike == 1 & Out_zone == 1 | Ball == 1 & In_zone == 1),

                     "stolenK" | "lostK" , 0))-> jorge

Error in "stolenK" | "lostK" :

operations are possible only for numeric, logical or complex types

I am confused on the second error as the variables are numerics

I am confused overall. I have never tried to combine two things in one if else and have no reference

Also, I do not know how mutate decides between stolen or lost. And truthfully I don’t have a clue.

would you mind offering a solution where you explain the answers to your questions. I understand what you are saying. first, is the first part of the statement correct?
mutate(result = ifelse(c(Called_Strike == 1 & Out_zone == 1 | Ball == 1 & In_zone == 1)
is that satisfactory or does it need adjustment? and second, if you would a solution w explanation... or more guided questions at least?
Thanks

Stephen

I tried the following and it only created 0s
jorge %>%
mutate(result = ifelse(c(Called_Strike == 1 & Out_zone == 1) ,
"stolenK" , 0) ,
result = ifelse(c(Ball == 1 & In_zone == 1) ,
"lostK" , 0)) -> jorge

You need an additional test to check whether been set to stolenK

sorry, but I do not understand:
I only need a check for stolenK , not lostK?
what would that look like?
thanks

Specifically what part of this step by step guide is causing you confusion?

In the first mutate, you set result to stolenK if the test was met. In the second mutate, which should be in the form of

mutate(result = ...),
mutate(result = ...)

you applied a different test, which the test for stokenK will fail, yes? So it defaults to 0.

what you want is something like

mutate(result = ifelse(Called_Strike == 1 & Out_zone == 1), "stolenK", 0) %>%
mutate(result = ifelse(result != "stolenK & Ball ==  & In_zone == 1, "lostK", 0)

It's always hepful to include reproducible example, called a reprex with a short data sample to help responses to understand question and clarify answers.

thanks so much. I just got to my baseball attempts again and read your answers. I appreciate your help. I am in over my head and get lost.

1 Like

Great! I'm glad that helped you find your way. Don't worry! It's easy to get lost in new features. I've been using R for over 10 years and I've gotten better at figuring out new packages and functions on my own, but I still go round and round at times before my aha! moment.

Also, you're using this community exactly the right way. Ask for help, clarification, then apply it on your own and report back. And keep in mind that often the person who answers what seem like simple question learns something new trying to explain it!

thanks for encouragement. I use it for baseball. there is so much data in baseball and many new interesting metrics: new class of statistics called saber. R is practically made just for it. I am currently working backwards from a script I copied. I understand most. It has some GAM models. I am going to see if my way will match my way. my way is always too long and harder than it should be.

I have just been using for about 9 months. I know too much of some and not enough of some basics. I am hoping to take class or classes soon. I live in Ny. would you know of anything that is serious and could lead to a certification or some sort of value?

thanks again.

1 Like

I recommend the HarvardX professional certificate in data science, a 9-module course from the basics through statistical applications and machine learning. Taught as an orientation course for graduate students on non-CS tracks who will still need to be doing data analysis. I took it last year.

3 posts were split to a new topic: Drawing rectangles with ggplot2