[R] If Loop I Think
Jim Lemon
drj|m|emon @end|ng |rom gm@||@com
Wed Oct 23 02:35:47 CEST 2019
Notice that I used the argument stringsAsFactors=FALSE to do this when
reading in.
What I did was to change Rn columns to 1 if there were any characters
in the corresponding Runnerxxx column and 0 otherwise. The "nchar"
function returns the number of characters in a string. If I apply ">0"
to it, I get TRUE(1) if nchar returns a number larger than zero and
FALSE (0) otherwise. By using the result of this operation as the
first argument in the call to "ifelse", I can set the values of the Rn
columns one by one. It is possible to do all three in one call with an
appropriate function:
whos_on_first<-function(x)
return(as.numeric(nchar(x) > 0))
phdf[,6:8]<-sapply(phdf[,3:5],whos_on_first)
Jiim
On Wed, Oct 23, 2019 at 11:20 AM Phillip Heinrich <herd_dog using cox.net> wrote:
>
> The routine you suggested worked once I changed RunnerFirst, Second, & Third
> to character vectors.
>
> But I really don't understand what the code is doing. I understand the
> Ifelse(no-character in RunnerFirst vector) but the 0,1,0 is a mystery. I
> assume the first zero is if the field is blank and a one if there is
> something in the field. But what does the third number do? And why is a >
> symbol used as opposed to an = sign?
>
> Again the bigger question is how do I learn this stuff? I bought another
> book, "R Projects for Dummies". I will work through the examples over the
> next week and hope I'll know more once I'm done.
>
> Can you suggest any other sources?
>
> Thanks.
>
> -----Original Message-----
> From: Jim Lemon
> Sent: Tuesday, October 22, 2019 3:26 PM
> To: Phillip Heinrich
> Cc: r-help
> Subject: Re: [R] If Loop I Think
>
> Hi Philip,
> Try this:
>
> phdf<-read.table(
> text="Row Outs RunnerFirst RunnerSecond RunnerThird R1 R2 R3
> 1 0
> 2 1
> 3 1
> 4 1 arenn001
> 5 2 arenn001
> 6 0
> 7 0 perad001
> 8 0 polla001 perad001
> 9 0 goldp001 polla001 perad001
> 10 0 lambj001 goldp001
> 11 1 lambj001 goldp001
> 12 2 lambj001
> 13 0
> 14 1 ",
> header=TRUE,stringsAsFactors=FALSE,fill=TRUE)
> phdf$R1<-ifelse(nchar(phdf$RunnerFirst) > 0,1,0)
> phdf$R2<-ifelse(nchar(phdf$RunnerSecond) > 0,1,0)
> phdf$R3<-ifelse(nchar(phdf$RunnerThird) > 0,1,0)
>
> Jim
>
> On Wed, Oct 23, 2019 at 7:54 AM Phillip Heinrich <herd_dog using cox.net> wrote:
> >
> > Row Outs RunnerFirst RunnerSecond RunnerThird R1 R2 R3
> > 1 0
> > 2 1
> > 3 1
> > 4 1 arenn001
> > 5 2 arenn001
> > 6 0
> > 7 0 perad001
> > 8 0 polla001 perad001
> > 9 0 goldp001 polla001 perad001
> > 10 0 lambj001 goldp001
> > 11 1 lambj001 goldp001
> > 12 2 lambj001
> > 13 0
> > 14 1
> >
> >
> >
> > With the above data, Arizona Diamondbacks baseball, I’m trying to put
> > zeros into the R1 column is the RunnerFirst column is blank and a one if
> > the column has a coded entry such as rows 4,5,7,8,& 9. Similarly I want
> > zeros in R2 and R3 if RunnerSecond and RunnerThird respectively are blank
> > and ones if there is an entry.
> >
> > I’ve tried everything I know how to do such as “If Loops”, “If-Then loops”,
> > “apply”, “sapply”, etc. I wrote function below and it ran without errors
> > but I have no idea what to do with it to accomplish my goal:
> >
> > R1 <- function(x) {
> > if (ari18.test3$RunnerFirst == " "){
> > ari18.test3$R1 <- 0
> > return(R1)
> > }else{
> > R1 <- ari18.test3$R1 <- 1
> > return(R1)
> > }
> > }
> >
> > The name of the data frame is ari18.test3
> >
> > On a more philosophical note, data handling in R seems to be made up of
> > thousands of details with no over-riding principles. I’ve read two books
> > on R and a number of tutorial and watched several videos but I don’t seem
> > to be making any progress. Can anyone suggest videos, or tutorials, or
> > books that might help? Database stuff has never been my strong point but
> > I’m determined to learn.
> >
> > Thanks,
> > Philip Heinrich
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list