[R] If Loop I Think

David Winsemius dw|n@em|u@ @end|ng |rom comc@@t@net
Wed Oct 23 00:29:06 CEST 2019


On 10/22/19 1:54 PM, Phillip Heinrich 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]]

I'm not sure how well you read instructions since you submitted this in 
HTML. The advice I would give for learning data handling is to study R's 
data structures and the help pages for `[<-`, `lapply`,`merge`, and the 
concept of vectorization. If you don't know Boolean logic well, then 
studying that topic would also be helpful. Studying the help page for 
`[<-` is a stepo may newbies think they can avoid, but mastery will 
elude you until you have read through it probably ten or tweny times.

Here's how to input that data and create R1. The methods to create 
R2,and R3 would take two more similar lines of code.

 > dat <- 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, fill=TRUE)
 > dat
    Row Outs RunnerFirst RunnerSecond RunnerThird R1 R2 R3
1    1    0                                      NA NA NA
2    2    1                                      NA NA NA
3    3    1                                      NA NA NA
4    4    1    arenn001                          NA NA NA
5    5    2    arenn001                          NA NA NA
6    6    0                                      NA NA NA
7    7    0    perad001                          NA NA NA
8    8    0    polla001     perad001             NA NA NA
9    9    0    goldp001     polla001    perad001 NA NA NA
10  10    0    lambj001     goldp001             NA NA NA
11  11    1    lambj001     goldp001             NA NA NA
12  12    2    lambj001                          NA NA NA
13  13    0                                      NA NA NA
14  14    1                                      NA NA NA
 > levels(dat$RunnerFirst)
[1] ""         "arenn001" "goldp001" "lambj001" "perad001" "polla001"
 > dat$R1 <- as.numeric( dat$RunnerFirst != "")
 > dat$R1
  [1] 0 0 0 1 1 0 1 1 1 1 1 1 0 0

-- 

David.


>
> ______________________________________________
> 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