[R] If Loop I Think

William Michels wjm1 @end|ng |rom c@@@co|umb|@@edu
Mon Oct 28 06:00:47 CET 2019


Hi Phillip,

I wanted to follow up with you regarding your earlier post. Below is a
different way to work up your data than I posted earlier.

I took the baseball data you posted, stripped out
leading-and-following blank lines, removed all trailing spaces on each
line, and removed the "R1", "R2" and "R3" column names, since they're
blank columns anyway. I then read this text file ("diamond2.txt") into
R using the read.table() call below. Note the use of the sep=" "
parameter--it is very important to include this parameter when
analyzing your dataset in R, as it is not the default setting. I was
then able to generate the "R1", "R2", "R3" columns you sought, using
apply() with anonymous functions:

> testAD <- read.table("diamond2.txt", header=T, sep=" ", na.strings="", fill=T, row.names=NULL, stringsAsFactors=F)
> testAD$R1=rep(NA, 14)
> testAD$R2=rep(NA, 14)
> testAD$R3=rep(NA, 14)
> testAD[ ,c(6:8)] <- apply(testAD[ ,c(3:5)], 2, FUN=function(x) {ifelse(test=nchar(x), yes=1, no=0)} )
> testAD[ ,c(6:8)] <- apply(testAD[ ,c(6:8)], 2, FUN=function(x) {ifelse(test=!is.na(x), yes=x, no=0)} )
> testAD
   Row Outs RunnerFirst RunnerSecond RunnerThird R1 R2 R3
1    1    0        <NA>         <NA>        <NA>  0  0  0
2    2    1        <NA>         <NA>        <NA>  0  0  0
3    3    1        <NA>         <NA>        <NA>  0  0  0
4    4    1    arenn001         <NA>        <NA>  1  0  0
5    5    2    arenn001         <NA>        <NA>  1  0  0
6    6    0        <NA>         <NA>        <NA>  0  0  0
7    7    0    perad001         <NA>        <NA>  1  0  0
8    8    0    polla001     perad001        <NA>  1  1  0
9    9    0    goldp001     polla001    perad001  1  1  1
10  10    0        <NA>     lambj001    goldp001  0  1  1
11  11    1        <NA>     lambj001    goldp001  0  1  1
12  12    2        <NA>         <NA>    lambj001  0  0  1
13  13    0        <NA>         <NA>        <NA>  0  0  0
14  14    1        <NA>         <NA>        <NA>  0  0  0
>

HTH,

Bill.

W. Michels, Ph.D.


On Thu, Oct 24, 2019 at 12:44 PM William Michels <wjm1 using caa.columbia.edu> wrote:
>
> Hi Phillip,



More information about the R-help mailing list