[R] "For" loop and "if" question

Steven Ranney steven.ranney at gmail.com
Wed Dec 19 21:56:54 CET 2012


A friendly r-helper asked me to use dput() to output some of my data.

The results are below:

structure(list(ID = 2004:2008, p1 = c(1L, 1L, 1L, 1L, 0L), p2 = c(0L,
0L, 0L, 0L, 1L), p3 = c(0L, 0L, 0L, 0L, 0L), p4 = c(0L, 0L, 1L,
1L, 0L), p5 = c(0L, 0L, 0L, 0L, 0L), p6 = c(1L, 1L, 0L, 1L, 1L
), p7 = c(0L, 0L, 1L, 1L, 0L), p8 = c(1L, 0L, 0L, 0L, 0L), p9 = c(0L,
0L, 1L, 1L, 0L), p10 = c(1L, 0L, 1L, 0L, 0L), p11 = c(0L, 0L,
0L, 1L, 0L), p12 = c(1L, 0L, 0L, 1L, 0L), p13 = c(0L, 0L, 1L,
0L, 0L), p14 = c(0L, 0L, 0L, 0L, 1L), p15 = c(0L, 0L, 1L, 1L,
0L), p16 = c(0L, 0L, 0L, 0L, 0L), p17 = c(0L, 1L, 0L, 0L, 0L),
    p18 = c(0L, 0L, 0L, 0L, 0L), p19 = c(0L, 0L, 0L, 0L, 1L),
    p29 = c(0L, 0L, 0L, 1L, 0L), p21 = c(0L, 1L, 0L, 1L, 0L),
    lat1 = c(29.266, 29.27057, 29.24012, 29.22798, NA), lat2 = c(NA,
    NA, NA, NA, 29.27332), lat3 = c(NA, NA, NA, NA, NA), lat4 = c(NA,
    NA, 29.23984, 29.26174, NA), lat5 = c(NA, NA, NA, NA, NA),
    lat6 = c(29.21016, 29.27799, NA, 29.24824, 29.27873), lat7 = c(NA,
    NA, 29.24511, 29.26614, NA), lat8 = c(29.27555, NA, NA, NA,
    NA), lat9 = c(NA, NA, 29.24437, 29.24437, NA), lat10 = c(29.26266,
    NA, 29.2633, NA, NA), lat11 = c(NA, NA, NA, 29.26547, NA),
    lat12 = c(29.26146, NA, NA, 29.2581, NA), lat13 = c(NA, NA,
    29.24212, NA, NA), lat14 = c(NA, NA, NA, NA, 29.27507), lat15 = c(NA,
    NA, 29.27403, 29.27403, NA), lat16 = c(NA, NA, NA, NA, NA
    ), lat17 = c(NA, 29.26448, NA, NA, NA), lat18 = c(NA, NA,
    NA, NA, NA), lat19 = c(NA, NA, NA, NA, 29.27167), lat20 = c(NA,
    NA, NA, 29.24208, NA), lat21 = c(NA, 29.27493, NA, 29.26212,
    NA), long1 = c(-89.96268, -89.95791, -90.00651, -90.02192,
    NA), long2 = c(NA, NA, NA, NA, -89.9543), long3 = c(NA, NA,
    NA, NA, NA), long4 = c(NA, NA, -90.00668, -89.98277, NA),
    long5 = c(NA, NA, NA, NA, NA), long6 = c(-90.0387, -89.95317,
    NA, -89.98473, -89.94817), long7 = c(NA, NA, -90.00278, -89.95956,
    NA), long8 = c(-89.9545, NA, NA, NA, NA), long9 = c(NA, NA,
    -90.02349, -90.02349, NA), long10 = c(-89.96309, NA, -89.96299,
    NA, NA), long11 = c(NA, NA, NA, -89.97777, NA), long12 = c(-89.97981,
    NA, NA, -89.99077, NA), long13 = c(NA, NA, -90.00163, NA,
    NA), long14 = c(NA, NA, NA, NA, -89.95477), long15 = c(NA,
    NA, -89.95444, -89.95444, NA), long16 = c(NA, NA, NA, NA,
    NA), long17 = c(NA, -89.96214, NA, NA, NA), long18 = c(NA,
    NA, NA, NA, NA), long19 = c(NA, NA, NA, NA, -89.95428), long20 = c(NA,
    NA, NA, -90.06522, NA), long21 = c(NA, -89.95477, NA, -89.98407,
    NA)), .Names = c("ID", "p1", "p2", "p3", "p4", "p5", "p6",
"p7", "p8", "p9", "p10", "p11", "p12", "p13", "p14", "p15", "p16",
"p17", "p18", "p19", "p29", "p21", "lat1", "lat2", "lat3", "lat4",
"lat5", "lat6", "lat7", "lat8", "lat9", "lat10", "lat11", "lat12",
"lat13", "lat14", "lat15", "lat16", "lat17", "lat18", "lat19",
"lat20", "lat21", "long1", "long2", "long3", "long4", "long5",
"long6", "long7", "long8", "long9", "long10", "long11", "long12",
"long13", "long14", "long15", "long16", "long17", "long18", "long19",
"long20", "long21"), row.names = c(NA, 5L), class = "data.frame")



Thanks again -

SR
Steven H. Ranney


On Wed, Dec 19, 2012 at 1:42 PM, Steven Ranney <steven.ranney at gmail.com> wrote:
> All -
>
> I have a large data frame that looks like
>
> ID     p1     p2     p3...p20     Lat1     Lat2     Lat3...Lat20
> Long1     Long2     Long3...Long20
> 1       0      0      1      0        NA       NA       29.xx NA
>  NA          NA         -89.xx   NA
> 2       1      0      0      1        27.xx   NA       NA     29.00
>  -88.00     NA         NA        -89.xx
> 3       0      0      0      0        NA      NA       NA      NA
>   NA         NA          NA       NA
> ...
> [truncated]
>
> where length(ID) = 1300 and the zeroes and ones in the p1-p20 column
> correspond to values in the Lat1-Lat20 and Long1-Long20 columns; a 0
> indicates no corresponding value and 1 indicates there is a
> corresponding value.
>
> I'd like to create a dataframe that is
>
> ID          p     Lat     Long
> 1           3     29.xx  -89.xx
> 2           1     27.xx  -88.xx
> 2           20   29.xx  -89.xx
> ...
>
> and so on, such that for every ID I have, there is a corresponding row
> with each Latn and Longn that may exist.
>
> My problem is that I don't know how to do that in R.  I can set up
> simple "for" loops, but I don't know how to set up something that has
> a "for" and an "if/else" statement.  And frankly, with the number of
> pX, LatX, and LongX, I think that my if/else statement would be huge
> and unwieldly.
>
> Could anyone offer any assistance?
>
> Thanks for your help -
>
> SR
> Steven H. Ranney




More information about the R-help mailing list