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

arun smartpink111 at yahoo.com
Thu Dec 20 13:58:03 CET 2012


HI,
You could replace the times=..... in res1 with:
res1<-na.omit(cbind(ID=rep(dat2[,2][grep("ID",dat2$variable)],times=length(grep("p",names(dat1)))),dat2[grep("p",dat2$variable),],dat2[grep("lat",dat2$variable),],dat2[grep("long",dat2$variable),]))

A.K.


----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: Steven Ranney <steven.ranney at gmail.com>
Cc: R help <r-help at r-project.org>
Sent: Wednesday, December 19, 2012 11:38 PM
Subject: Re: [R] "For" loop and "if" question



Hi,
You could also try this:
#dat1<-structure.... BTW, p20 was labelled incorrectly as p29
library(reshape)
 dat2<-melt(dat1)
res1<-na.omit(cbind(ID=rep(dat2[,2][grep("ID",dat2$variable)],times=((nrow(melt(dat1))-nrow(dat1))/3)/nrow(dat1)
),dat2[grep("p",dat2$variable),],dat2[grep("lat",dat2$variable),],dat2[grep("long",dat2$variable),]))
res1<-res1[,-c(3,4,6)]
 res1[,2]<-as.numeric(gsub("\\D+","",res1[,2]))
 colnames(res1)[2:4]<-c("p","lat","long")
 row.names(res1)<-1:nrow(res1)
 head(res1)
#    ID p      lat      long
#1 2004 1 29.26600 -89.96268
#2 2005 1 29.27057 -89.95791
#3 2006 1 29.24012 -90.00651
#4 2007 1 29.22798 -90.02192
#5 2008 2 29.27332 -89.95430
#6 2006 4 29.23984 -90.00668
identical(res1[,-1],fun(dat1))
#[1] TRUE

A.K.






----- Original Message -----
From: Rui Barradas <ruipbarradas at sapo.pt>
To: Steven Ranney <steven.ranney at gmail.com>
Cc: r-help at r-project.org
Sent: Wednesday, December 19, 2012 6:16 PM
Subject: Re: [R] "For" loop and "if" question

Hello,

Thanks for the data example.
Try the following.


fun <- function(DF){
     f <- function(x, y){
         if(ncol(y) == 3) rbind(x, y) else x
     }

     p <- grep("^p[[:digit:]]+$", names(DF))
     lat <- grep("^lat[[:digit:]]+$", names(DF))
     long <- grep("^long[[:digit:]]+$", names(DF))
     p <- sapply(DF[p], as.logical)
     lat <- sapply(DF[lat], as.numeric)
     long <- sapply(DF[long], as.numeric)

     tmp <- sapply(seq_len(ncol(p)), function(j)
         cbind(p = j, lat = lat[p[, j], j], long = long[p[, j], j]))
     tmp <- na.omit(Reduce(f, tmp))
     data.frame(tmp, row.names = seq_len(nrow(tmp)))
}

fun(dat)  # 'dat' is your data example, dat <- structure(...)


Hope this helps,

Rui Barradas
Em 19-12-2012 20:56, Steven Ranney escreveu:
> 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
> ______________________________________________
> R-help at r-project.org mailing list
> 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.

______________________________________________
R-help at r-project.org mailing list
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.


______________________________________________
R-help at r-project.org mailing list
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