Re: [R] help identifying clusters
Look at clustering task view http://cran.r-project.org/web/views/Cluster.html A simple way to do it is library(cluster) dat$cluster <- pam(dat, 3, stand=T)$clustering plot(dat$lon, dat$lat, col=dat$cluster) Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com On Oct 20, 2010, at 6:41 PM, jonas garcia wrote: dat<- data.frame( lon = c(rnorm(1000, mean=-10), rnorm(1000, mean=10), rnorm(1000, mean=5)), lat = c(rnorm(1000, mean=40), rnorm(1000, mean=30), rnorm(1000, mean=0))) plot(dat$lon, dat$lat) __ R-help@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.
Re: [R] Using as.polynomial() over a matrix
it looks like as. polynomial merely stores the coefficients of the polynomial. "Internally, polynomials are simply numeric coefficient vectors of class "polynomial". " If you really want to print it in the form you want try this. m <- matrix(rnorm(3000),1000,3) apply(m, 1, function(x){print(as.polynomial(x))}) # Not sure why you are using 2 i.e. applying the formula column wise instead of 1, row wise. Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com On Oct 4, 2010, at 9:10 PM, Raznahan, Armin (NIH/NIMH) [E] wrote: > Hello All > > First - a warning. I'm not very R or programming savvy. > > I am trying to do something without much luck, and have scoured help- > pages, but nothing has come up. Here it is: > > I have a matrix (m) of approx 40,000 rows and 3 columns, filled with > numbers. > > I would like to convert the contents of this matrix into another > matrix (m_p), where the numbers of (m) have been coerced into a > polynomial - using a function called "as.polynomial()" from the > package (polynom). Each row of (m) contains 3 terms to be made into > a polynomial in the equivalent row of (m_p). > > I have tried a coupe of things: > > -- > 1. Using apply() > > m_p<-apply(m, 2, as.polynomial) > > Here is what happens.. > >> dim(m) > [1] 40962 3 >> m_p<-apply(m, 2, as.polynomial) >> m_p[1:5,] >dM_IdM_a.c dM_a.c.sq > [1,] -0.00593058 -0.000688 3.65e-05 > [2,] -0.01913294 0.000103 1.41e-04 > [3,] -0.01317958 -0.001190 1.49e-04 > [4,] -0.02651112 -0.001550 2.37e-04 > [5,] -0.01680289 -0.003520 2.86e-04 > > So - looks like the coercion hasn't worked. BUT, if I do things > piecemeal - it looks ok.. > >> m_p1<-as.polynomial(m[1,]) >> m_p1 > -0.00593058 - 0.000688*x + 3.65e-05*x^2 > > --- > 2. This made me think I was making some wrong assumptions using > apply(). So I wrote a function "test()", to take each row of (m) , > use as.polynomial() on it, and stick the results into a new matrix, > which it would then return.. > > test<-function(x){ > a<-nrow(x) > b<-ncol(x) > c<-matrix(0, a, b) > for (i in 1:a) { > c[i,]<-as.polynomial(x[i,]) } > return (c) > } > >> m_p<-test(m) >> dim(m_p) > [1] 40962 3 >> m_p[1:5,] >[,1] [,2] [,3] > [1,] -0.00593058 -0.000688 3.65e-05 > [2,] -0.01913294 0.000103 1.41e-04 > [3,] -0.01317958 -0.001190 1.49e-04 > [4,] -0.02651112 -0.001550 2.37e-04 > [5,] -0.01680289 -0.003520 2.86e-04 > > --- > > I don't know why I can do what I want when taking each line at a > time, but not when trying to run through the whole matrix. > > Sorry if missing something obvious. Any help/pointers would be very > gratefully received > > Thanks v much > > Armin > > __ > R-help@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. [[alternative HTML version deleted]] __ R-help@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.
Re: [R] removed data is still there!
example(factor) iris1$Species <- factor(iris1$Species, drop=T) will get you what you need. Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com On Sep 21, 2010, at 7:41 AM, pdb wrote: I'm confused, hope someone can point out what is not obvious to me. I thought I was creating a new data frame by 'deleting' rows from an existing dataframe - I've tried 2 methods. But this new data frame seems to remember values from its parent - even though there are no occurences. Where does it get the values versicolor and virginica from and give then a count of 0? What am I missing? Thanks in advance. summary(iris$Species) setosa versicolor virginica 50 50 50 nrow(iris) [1] 150 iris1 <- iris[iris$Species == 'setosa',] nrow(iris1) [1] 50 summary(iris1$Species) setosa versicolor virginica 50 0 0 boxplot(Petal.Width ~ Species, data = iris1, plot=1) iris2 <- subset(iris, Species == 'setosa') nrow(iris2) [1] 50 summary(iris2$Species) setosa versicolor virginica 50 0 0 boxplot(Petal.Width ~ Species, data = iris2, plot=1) -- View this message in context: http://r.789695.n4.nabble.com/removed-data-is-still-there-tp2548440p2548440.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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@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.
Re: [R] Finding (Ordered Subvectors)
Convert to strings and use grep functions. using c for variable is a bad idea. a <- paste(a, collapse="") b <- paste(b, collapse="") d <- paste(d, collapse="") grepl(b,a) grepl(d,a) Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com On Sep 21, 2010, at 6:31 AM, Lorenzo Isella wrote: a<-c(1,4,3,0,4,5,6,9,3,4) b<-c(0,4,5) c<-c(5,4,0) __ R-help@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.
Re: [R] how to compute when row length is different
DF3 <- merge(DF1, DF2, by=c("Sample_id", "RepairHours), all.y=T) DF3$subtract <- DF3$Day_0_Read1-DF3$ ZeroMean Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com On Sep 14, 2010, at 8:38 AM, rasanpreet wrote: hi guys..please help me with this i am working on two data frames one goes like this: DF1 Sample_id RepairHours Denatured Dose ZeroMean FourtyFiveMean NinetyMean 1 SDM071 0 1B 60.5 19.0 45.0 2 SDM071 1 1B 46.0 23.0 42.5 3 SDM071 2 1B 52.5 24.0 40.0 4 SDM071 3 1B 42.0 21.5 45.0 5 SDM053 0 1B 66.5 28.5 56.5 6 SDM053 1 1B 47.0 29.0 47.5 7 SDM053 2 1B 52.0 31.0 44.0 8 SDM053 3 1B 36.0 34.0 41.5 9 SDM059 0 1B 47.5 41.5 29.0 10SDM059 1 1B 47.0 36.0 35.0 11SDM059 2 1B 41.5 42.0 32.5 12SDM059 3 1B 46.5 41.5 32.0 and the other one: DF2 SampleId RepairHours Denatured Dose_uM Day_0_Read1 Day_0_Read2 Day_45_Read1 8SDM071 0 1 C 124 120 108 9SDM071 0 1 25 123 128 77 10 SDM071 0 1 50 132 138 79 11 SDM071 0 1 100 118 116 68 12 SDM071 0 1 200 125 146 73 20 SDM071 1 1 C 113 117 113 21 SDM071 1 1 25 108 115 132 22 SDM071 1 1 50 105 96 94 23 SDM071 1 1 100 101 101 88 24 SDM071 1 1 200 114 106 89 32 SDM071 2 1 C 143 136 109 33 SDM071 2 1 25 126 147 110 34 SDM071 2 1 50 109 122 107 35 SDM071 2 1 100 114 118 89 36 SDM071 2 1 200 118 128 88 44 SDM071 3 1 C 103 111 116 45 SDM071 3 1 25 108 105 115 46 SDM071 3 1 50 118 99 88 47 SDM071 3 1 100 98 103 105 48 SDM071 3 1 200 112 105 96 56 SDM053 0 1 C 214 208 158 57 SDM053 0 1 25 159 214 178 58 SDM053 0 1 50 170 169 112 59 SDM053 0 1 100 149 158 124 60 SDM053 0 1 200 201 171 115 68 SDM053 1 1 C 149 166 120 69 SDM053 1 1 25 145 134 118 70 SDM053 1 1 50 159 169 130 71 SDM053 1 1 100 113 126 110 72 SDM053 1 1 200 118 112 120 these are just part of the frames.. i have to subtract the first five values of dataframe2 from one value from dataframe1 eg: subtract<-DF2$Day_0_Read1-DF1$ ZeroMean if u notice the repair hours in both have to match...along with their id's. i have tried this zeroday_subtract1=DF1$Day_0_Read1 - DF2[DF1$RepairHours,]$ZeroMean but it dosent work please help me with this...i know its basic but i needhelp thx in advance -- View this message in context: http://r.789695.n4.nabble.com/how-to-compute-when-row-length-is-different-tp2538930p2538930.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]] __ R-help@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@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.
Re: [R] problem with max in a function
Comments below On Sep 7, 2010, at 9:37 PM, stephen sefick wrote: Here is a striped down example that is not working because of the 1.00 to 1. Any help would be greatly appreciated. measure_bkf <- (structure(list(measurment_num = c(0, 0.2, 0.4, 0.6, 0.8, 1, 1.2, 1.4, 1.6, 1.8, 2, 2.2, 2.2, 2.4, 2.6, 2.8), bankfull_depths_m = c(-0.15, -0.09, -0.00998, 0.06, 0.13, 0.26, 0.36, 0.46, 0.56, 0.61, 0.85, 0.93, 0.93, 0.97, 1, 1)), .Names = c("measurment_num", "bankfull_depths_m"), row.names = c(32L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 29L, 12L, 13L, 14L), class = "data.frame")) measure_bkf_not_zero <- measure_bkf[grep("[^0]", measure_bkf[,"bankfull_depths_m"]),] If I understand your intent correctly You can just use (of course be vary of the floating point arithmetic) measure_bkf_not_zero <- subset(measure_bkf, measure_bkf $bankfull_depths_m != 0) bkf_min <- grep(min(measure_bkf_not_zero[,"bankfull_depths_m"]), measure_bkf_not_zero[,"bankfull_depths_m"]) bkf_max <- grep(max(measure_bkf_not_zero[,"bankfull_depths_m"]), measure_bkf_not_zero[,"bankfull_depths_m"]) Are you looking for which. min and which.max? or just min and max? bkf_min <- ifelse(length(bkf_min)>1, bkf_min[1], bkf_min) bkf_max <- ifelse(length(bkf_max)>1, bkf_max[1], bkf_max) This is unnecessary if you are using min and max as they just return one value. #s <- with(measure_bkf_not_zero, approx(measurment_num, bankfull_depths_m, xout=seq(measure_bkf_not_zero[bkf_min,"measurment_num"], measure_bkf_not_zero[bkf_max,"measurment_num"], length=2000))) #int_bkf <- with(s, x[which.min(y[y>0])]) s <- with(measure_bkf_not_zero[bkf_min:bkf_max,], approxfun(bankfull_depths_m, measurment_num), ties=mean) int_bkf <- s(0) On Tue, Sep 7, 2010 at 8:28 PM, David Winsemius > wrote: On Sep 7, 2010, at 9:06 PM, stephen sefick wrote: s <- 1.00 max(s) sprintf("%.2f", max(s)) [1] "1.00" @ as a string/character object returns 1 is there anyway that I can get it to return 1.00. I am using the results of this max statement in a grep statement and it returns the wrong numbers, I will provide more information and code if it would make more sense in context. -- Stephen Sefick | Auburn University | | Department of Biological Sciences | | 331 Funchess Hall | | Auburn, Alabama | | 36849| |___| | sas0...@auburn.edu | | http://www.auburn.edu/~sas0025 | |___| Let's not spend our time and resources thinking about things that are so little or so large that all they really do for us is puff us up and make us feel like gods. We are mammals, and have not exhausted the annoying little problems of being mammals. -K. Mullis "A big computer, a complex algorithm and a long time does not equal science." -Robert Gentleman __ R-help@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. David Winsemius, MD West Hartford, CT -- Stephen Sefick | Auburn University | | Department of Biological Sciences | | 331 Funchess Hall | | Auburn, Alabama | | 36849| |___| | sas0...@auburn.edu | | http://www.auburn.edu/~sas0025 | |___| Let's not spend our time and resources thinking about things that are so little or so large that all they really do for us is puff us up and make us feel like gods. We are mammals, and have not exhausted the annoying little problems of being mammals. -K. Mullis "A big computer, a complex algorithm and a long time does not equal science." -Robert Gentleman __ R-help@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@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.
Re: [R] How to remove all objects except a few specified objects?
or use #not checked rm(setdiff(ls(),c("a", "b")) On Aug 24, 2010, at 4:55 AM, Barry Rowlingson wrote: 2010/8/24 500600 : a <- 1 b <- 2 c <- 3 ls()[-a] # set minus to all the objects you want to retain rm(list = ls()[-a] # will remove all the objects - except a ls() # presto Only because a=1 and a is the first item in the list! Not because you are doing '-a'! If a is 0 then nothing gets deleted, and if a isn't numeric vector then it just fails. If you want to do it by name, use match Barry __ R-help@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@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.
Re: [R] Quantile Regression and Goodness of Fit
http://www.econ.uiuc.edu/~roger/research/R1/R1.html On Mon, Aug 23, 2010 at 2:15 PM, Steven Ranney wrote: > All - > > Does anyone know if there is a method to calculate a goodness-of-fit > statistic for quantile regressions with package quantreg? > Specifically, I'm wondering if anyone has implemented the > goodness-of-fit process developed by Koenker and Machado (1999) for R? > > Though I have used package quantreg in the past, I may have overlooked > this function, if it is included. > > Citation: > > Koenker, R. and J. A. F. Machado. 1999. Goodness of fit and related > inference processes for quantile regression. Journal of the American > Statistical Association 94:1296-1310. > > Thank you - > > Steven H. Ranney > Graduate Research Assistant (Ph.D.) > > USGS MT Cooperative Fishery Research Unit > Montana State University > PO Box 173460 > Bozeman, MT 59717 > > office: 406-994-6643 > fax:406-994-7479 > > http://studentweb.montana.edu/steven.ranney > > __ > R-help@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. > [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Help with Vectors and conditional functions
In additiion to Ivan's comment, in this case, you are just plotting Yes or No. I think thats not what you want. Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com On Aug 19, 2010, at 3:42 AM, Ivan Calandra wrote: Hi, I haven't spent too much time on it, but that might help: if (coint_tests[[i]]=="YES")... Note the double "=" which is the operator for equality. The single "=" is the assignment operator, especially with arguments. HTH, Ivan Le 8/19/2010 09:03, Ferreira, Thiago Alves a écrit : Good morning, I have something like this: names(coint_tests)<- apply(b,2,paste, collapse="_") which prints 15 names like: A_B, C_D, E_F, ... AA,B,C,D.. Are time series. Then there is a vector called coint_tests of length 15 which yields "yes" or "no". I need to add a function to plot the time series Ai_Bi if the coint_tests vectors gives me a "YES". I tried:for (i in 1:(length(coint_tests)) if (coint_tests[[i]]="YES") {plot(coint_tests[i])} But it does not work. I would most appreciate if someone could give me some insight as to how to sort this out. Thank you __ R-help@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. -- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. Säugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calan...@uni-hamburg.de ** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php [[alternative HTML version deleted]] __ R-help@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@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.
Re: [R] replace loops with matrix
what is nt? is that a typo for ns? I don't see why you need to calculate lia within the loop. Also library(fBasics) ccl <-rowprod(lia) Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com On Aug 17, 2010, at 6:22 PM, Hey Sky wrote: Hey, R users I am using numerical method for my research paper and the computation burden is very heavy. first I tried to do it with loops, example code as following, and it take hours to converge for only 200 obs. and my real data has 4000 obs. and the optimization command that I use is: optim(guess,myfunc1,data=mydata, method="BFGS",hessian=T)) then I tried matrix form computation, it takes only 1/10 of the time the loop method takes. it may still have room to improve it. at least, the following part looks ugly. ccl[,m]<-lia[,1]*lia[,2]*lia[,3]*lia[,4]*lia[,5] any suggestion are appreciated. The Loop code: for(m in 1:ns){ for(i in 1:nt){ vbar2[,i]=a[1]+ eta[m]+acedu[,i]*a[2]+acwrk[,i]*a[3] vbar3[,i]=b[1]+b[2]*eta[m]+acedu[,i]*b[3]+acwrk[,i]*b[4] v8[,i]=1+exp(vbar2[,i])+exp(vbar3[,i]) for(j in 1:n){ if (edu[j,i]==1) lia[j,i]=1/v8[j,i] if (wrk[j,i]==1) lia[j,i]=exp(vbar2[j,i])/v8[j,i] if (home[j,i]==1) lia[j,i]=exp(vbar3[j,i])/v8[j,i] } ccl[,m]<-lia[,i]*ccl[,m] } } The Matrix code: for(m in 1:ns){ vbar2[,1:nt]=a[1]+ eta[m]+acedu[,1:nt]*a[2]+acwrk[,1:nt]*a[3] vbar3[,1:nt]=b[1]+b[2]*eta[m]+acedu[,1:nt]*b[3]+acwrk[,1:nt]*b[4] v8[,1:nt]=1+exp(vbar2[,1:nt])+exp(vbar3[,1:nt]) lia[1:n,]<-ifelse(edu[1:n,]==1,1/v8[1:n,], ifelse(wrk[1:n,]==1,exp(vbar2[1:n,])/ v8[1:n,], exp(vbar3[1:n,])/v8[1:n,])) ccl[,m]<-lia[,1]*lia[,2]*lia[,3]*lia[,4]*lia[,5] } Nan from Montreal __ R-help@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@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.
Re: [R] Equality of Vectors
?all nikhil.l...@gmail.com On Aug 13, 2010, at 2:49 PM, Downey, Patrick wrote: c(1,2,3) == c(1,2,3) __ R-help@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.
Re: [R] Fw: Error in rowSums REPOST
?as.numeric On Aug 13, 2010, at 7:50 AM, Amit Patel wrote: For the query below I have also included the follwing information. Thanks for your replies str(FeaturePresenceMatrix) chr [1:65530, 1:40] "0" "0" "0" "0" "1" "0" "0" "0" "0" ... - attr(*, "dimnames")=List of 2 ..$ : chr [1:65530] "4" "5" "6" "7" ... ..$ : chr [1:40] "X1" "X2" "X3" "X4" ... ?class class(FeaturePresenceMatrix) [1] "matrix" Amit Patel wrote: Hi I am trying to calculate the row sums of a matrix i have created The matrix ( FeaturePresenceMatrix) has been created by 1) Read csv 2) Removing unnecesarry data using [-1:4,] command 3) replacing all the NA values with as.numeric(0) and all others with as.numeric (1) When I carry out the command TotalFeature <- rowrowSums(FeaturePresenceMatrix, na.rm = TRUE) I get the following error. Error in rowSums(FeaturePresenceMatrix, na.rm = TRUE) : 'x' must be numeric Any tips onhow I can get round this? Yes, follow the posting guide and give the list a reproducible example. We don't know a critical piece of information, the class of your data. We know it's *not* numeric though, which is what it needs to be. Use ?class, ?str, and possibly give us a small sample with ?dput. That way, we can reproduce the error. __ R-help@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@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.
Re: [R] Help with permutation / loops
The adfresd function that you have, prints the outcome of the test rather than `return' ing a value. If you would modify that function to return a value (True or false/ or p-value) you should automatically get vector that you desire. Then is a simple task of naming the resultant vector with. apply(b,2, paste, collapse="_") On Thu, Aug 12, 2010 at 10:15 AM, Ferreira, Thiago Alves < thiago.alves.ferre...@citi.com> wrote: > Hello Nikhil, hope you are well today. > I am sorry to be a pain but I have one follow up question, I am trying to > express my results in a grid, which would look like a 6 by 6 matrix and > would have just Yes or NO in each grid.. > So what I am thinking is a way to store every result I get on " > apply(b,2,coint) " in an array or vector, and express them is this matrix. > So that I can just look at it and see whether assets swap2 and vol are > cointegrated.. > > Do you reckon you point me in the right direction as to how to do that? > > Thank you! > Thiago > > -Original Message- > From: Nikhil Kaza [mailto:nikhil.l...@gmail.com] > Sent: 11 August 2010 12:35 > To: Ferreira, Thiago Alves [ICG-MKTS] > Cc: 'r-help@R-project.org' > Subject: Re: [R] Help with permutation / loops > > How about this? > > untested since no data is provided. > > a <- paste("x",1:6,sep="") > b<- combn(a,2) > > > coint<-function (x) > > { >x1 <- get(x[1]) >x2 <- get(x[2]) > >adfdata(x1) > >adfdata(x2) > > > >engle<-lm(x1~x2) > >residual<-resid(engle) > >adfresd(residual, k=1) > > > >par(mfrow=c(2,1)) > >ts.plot(x1) > >ts.plot(x2) > > ts.plot(residual) } > > > apply(b,2,coint) > > > Careful with the plots, you may just overwrite them on default device. > One way to overcome it is to plot them to a pdf and name them appropriately > for each iteration. > > Nikhil Kaza > Asst. Professor, > City and Regional Planning > University of North Carolina > > nikhil.l...@gmail.com > > On Aug 11, 2010, at 6:34 AM, Ferreira, Thiago Alves wrote: > > > Hi everyone, > > I am writing a code for cointegration between n (n=6 initially) pairs. > > > > I have done the whole thing for 2 variables. There is a function > > coint(x1,x2) that takes 2 inputs x1 and x2 and does the following: > > > > coint<-function (x1,x2) > > { > >adfdata(x1) > >adfdata(x2) > > > >engle<-lm(x1~x2) > >residual<-resid(engle) > >adfresd(residual, k=1) > > > >par(mfrow=c(2,1)) > >ts.plot(x1) > >ts.plot(x2) > >ts.plot(residual) } > > > > Where X1,x2,..,x6 are time series of length 250 or more > > > > Where adfdata() is a function that calculates the adf test for x1 and > > x2. There are 6 variables in total (x1,x2,...,x6) and I want to > > calculate this function coint for the permutation of these variables. > > That is coint(x1,x2); coint(x1,x3); > > coint(x1,x4);...coint(x6,x5) (without repetition because x1,x1 are > > cointegrated already) > > > > I thought about creating an array with the combinations Xi,Xj and > > apply the function to each combination in the array but I could not > > get it to work... > > I would really appreciate if someone could help me on this! > > > > Thank you, > > Kind regards, > > Thiago > > > > __ > > R-help@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. > > [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Help with permutation / loops
How about this? untested since no data is provided. a <- paste("x",1:6,sep="") b<- combn(a,2) coint<-function (x) { x1 <- get(x[1]) x2 <- get(x[2]) adfdata(x1) adfdata(x2) engle<-lm(x1~x2) residual<-resid(engle) adfresd(residual, k=1) par(mfrow=c(2,1)) ts.plot(x1) ts.plot(x2) ts.plot(residual) } apply(b,2,coint) Careful with the plots, you may just overwrite them on default device. One way to overcome it is to plot them to a pdf and name them appropriately for each iteration. Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com On Aug 11, 2010, at 6:34 AM, Ferreira, Thiago Alves wrote: Hi everyone, I am writing a code for cointegration between n (n=6 initially) pairs. I have done the whole thing for 2 variables. There is a function coint(x1,x2) that takes 2 inputs x1 and x2 and does the following: coint<-function (x1,x2) { adfdata(x1) adfdata(x2) engle<-lm(x1~x2) residual<-resid(engle) adfresd(residual, k=1) par(mfrow=c(2,1)) ts.plot(x1) ts.plot(x2) ts.plot(residual) } Where X1,x2,..,x6 are time series of length 250 or more Where adfdata() is a function that calculates the adf test for x1 and x2. There are 6 variables in total (x1,x2,...,x6) and I want to calculate this function coint for the permutation of these variables. That is coint(x1,x2); coint(x1,x3); coint(x1,x4);...coint(x6,x5) (without repetition because x1,x1 are cointegrated already) I thought about creating an array with the combinations Xi,Xj and apply the function to each combination in the array but I could not get it to work... I would really appreciate if someone could help me on this! Thank you, Kind regards, Thiago __ R-help@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@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.
Re: [R] efficient matrix element comparison
How about a <- which(row(matchM)!=matchM) b <- matchM[a] diag(collusionM[a,b]) <-1 Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com On Aug 8, 2010, at 8:43 PM, david h shanabrook wrote: It is a simple problem in that I simply want to convert the For loop to a more efficient method. It simply loops through a large vector checking if the numeric element is not equal to the index of that element. The following example demonstrates a simplified example: rows <- 10 collusionM <- Matrix(0,10,10,sparse=TRUE) matchM <- matrix(c(1,2,3,4,4,6,7,9,9,10)) for (j in 1:rows) if (j != matchM[j]) collusionM[j,matchM[j]] <- collusionM[j,matchM[j]]+1 collusionM 10 x 10 sparse Matrix of class "dgCMatrix" [1,] . . . . . . . . . . [2,] . . . . . . . . . . [3,] . . . . . . . . . . [4,] . . . . . . . . . . [5,] . . . 1 . . . . . . [6,] . . . . . . . . . . [7,] . . . . . . . . . . [8,] . . . . . . . . 1 . [9,] . . . . . . . . . . [10,] . . . . . . . . . . Again, this works, I just need the for loop to be more efficient as in my application rows=37000, and I need to do this 100 times. Thanks. [[alternative HTML version deleted]] __ R-help@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@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.
Re: [R] Best way to Convert String to Time for comparison?
Another way is t1<- c("3:00","1:59","3:00","2:00") t2 <- strptime(t1, format="%H:%M") t2[-4]>t2[-1] Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com On Aug 4, 2010, at 8:47 AM, Gabor Grothendieck wrote: On Wed, Aug 4, 2010 at 8:43 AM, allany wrote: Hi guys, I have a large text file with a bunch of Time in "HH:MM" format, what would be the best way to process it into a Time Object so that I can use comparisons like (1:00<"1:15") or (13:00>"2:00") to both return true. Right now if I do a comparison like (3:00 < "1:59") I get a true, but if I do (3:00 < "2:00") I get false, which is an obvious error. Try this: library(chron) t1 <- times(paste("13:00", "00", sep = ":")) t2 <- times(paste("2:00", "00", sep = ":")) t1 > t2 __ R-help@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@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.
Re: [R] Plotting multiple layers(maps) on same page
you will have a better luck with R-sig-geo. Unfortunately I could not find an easy way for polygon overlays. plot(Tazshape) lines(ugbshape) if ugbshape was a polyline instead of a polygon. Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com On Aug 3, 2010, at 12:54 PM, LCOG1 wrote: Hey guys and gals, I searched through the forum and a bunch of R-mapping dedicated sites but have not found what i know is quite elementary process, mapping more than one layer on the same plot. I need to show some reference lines for the map to make sense. I know the below wont work for anyone but to show what im trying to do: TazFile <- "data/input/TAZ.shp" UGBFile<- "data/input/metugb.shp" TazShape <- readShapeSpatial(TazFile) UGBShape <- readShapeSpatial(UGBFile) #Write out to pdf pdf("Results/HhEmpForecast.pdf", width=8.2, height=11, onefile=TRUE) #Households plot(TazShape, col=colsHh[findInterval(TazShape$Hh, brksHh, all.inside=TRUE)]) plot(UGBShape) title(main="Households Forecast by Taz") legend(4199277,860791.2,title="Total Households by Taz",LegBrksHh,fill=colsHh,cex=.8) #Employment plot(TazShape, col=colsEmp[findInterval(TazShape$Emp, brksEmp, all.inside=TRUE)]) title(main="Employment Forecast by Taz") legend(4199277,860791.2,title="Total Employment by Taz",LegBrksEmp,fill=colsEmp,cex=.8) #Close file dev.off() I need Households and employment on separate maps but i need the UGB file to be on each for reference. Doing it the above way obviously puts the reference map (UGB) on a separate map. So how do i plot them on the same page. I thought this was easy enough but im not finding a simple answer. Thanks for the help. In Solidarity, JR -- View this message in context: http://r.789695.n4.nabble.com/Plotting-multiple-layers-maps-on-same-page-tp2312223p2312223.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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@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.
Re: [R] Need help on upper.tri()
try using Matrix package instead mat <- Matrix(rnorm(25),5,5) forceSymmetric(mat) The reason your method does not work is because matrix is effectively a vector and the indices increase along rows within a column. Nikhil On Aug 3, 2010, at 7:36 AM, Ron Michael wrote: HI, I am really messing up to make a symmetrical matrix using upper.tri() & lower.tri() function. Here is my code: set.seed(1) mat = matrix(rnorm(25), 5, 5) mat [,1] [,2] [,3][,4][,5] [1,] -0.6264538 -0.8204684 1.5117812 -0.04493361 0.91897737 [2,] 0.1836433 0.4874291 0.3898432 -0.01619026 0.78213630 [3,] -0.8356286 0.7383247 -0.6212406 0.94383621 0.07456498 [4,] 1.5952808 0.5757814 -2.2146999 0.82122120 -1.98935170 [5,] 0.3295078 -0.3053884 1.1249309 0.59390132 0.61982575 mat[lower.tri(mat)] = mat[upper.tri(mat)] mat [,1][,2][,3][,4][,5] [1,] -0.62645381 -0.82046838 1.51178117 -0.04493361 0.91897737 [2,] -0.82046838 0.48742905 0.38984324 -0.01619026 0.78213630 [3,] 1.51178117 -0.01619026 -0.62124058 0.94383621 0.07456498 [4,] 0.38984324 0.94383621 0.78213630 0.82122120 -1.98935170 [5,] -0.04493361 0.91897737 0.07456498 -1.98935170 0.61982575 Which is not coming as symmetrical function. Can anyone point me on the correct way of using upper, lower.try() function to get a symmetrical matrix? Thanks, [[alternative HTML version deleted]] __ R-help@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@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.
Re: [R] Collinearity in Moderated Multiple Regression
My usual strategy of dealing with multicollinearity is to drop the offending variable or transform one them. I would also check vif functions in car and Design. I think you are looking for lm.ridge in MASS package. Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com On Aug 3, 2010, at 9:51 AM, haenl...@gmail.com wrote: I'm sorry -- I think I chose a bad example. Let me start over again: I want to estimate a moderated regression model of the following form: y = a*x1 + b*x2 + c*x1*x2 + e Based on my understanding, including an interaction term (x1*x2) into the regression in addition to x1 and x2 leads to issues of multicollinearity, as x1*x2 is likely to covary to some degree with x1 (and x2). One recommendation I have seen in this context is to use mean centering, but apparently this does not solve the problem (see: Echambadi, Raj and James D. Hess (2007), "Mean-centering does not alleviate collinearity problems in moderated multiple regression models," Marketing science, 26 (3), 438 - 45). So my question is: Which R function can I use to estimate this type of model. Sorry for the confusion caused due to my previous message, Michael On Aug 3, 2010 3:42pm, David Winsemius wrote: I think you are attributing to "collinearity" a problem that is due to your small sample size. You are predicting 9 points with 3 predictor terms, and incorrectly concluding that there is some "inconsistency" because you get an R^2 that is above some number you deem surprising. (I got values between 0.2 and 0.4 on several runs. Try: x1 x2 x3 y model summary(model) # Multiple R-squared: 0.04269 -- David. On Aug 3, 2010, at 9:10 AM, Michael Haenlein wrote: Dear all, I have one dependent variable y and two independent variables x1 and x2 which I would like to use to explain y. x1 and x2 are design factors in an experiment and are not correlated with each other. For example assume that: x1 x2 cor(x1,x2) The problem is that I do not only want to analyze the effect of x1 and x2 on y but also of their interaction x1*x2. Evidently this interaction term has a substantial correlation with both x1 and x2: x3 cor(x1,x3) cor(x2,x3) I therefore expect that a simple regression of y on x1, x2 and x1*x2 will lead to biased results due to multicollinearity. For example, even when y is completely random and unrelated to x1 and x2, I obtain a substantial R2 for a simple linear model which includes all three variables. This evidently does not make sense: y model summary(model) Is there some function within R or in some separate library that allows me to estimate such a regression without obtaining inconsistent results? Thanks for your help in advance, Michael Michael Haenlein Associate Professor of Marketing ESCP Europe Paris, France [[alternative HTML version deleted]] __ R-help@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. David Winsemius, MD West Hartford, CT [[alternative HTML version deleted]] __ R-help@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@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.
Re: [R] Collinearity in Moderated Multiple Regression
Are x1 and x2 are factors (dummy variables)? cor does not make sense in this case. Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com On Aug 3, 2010, at 9:10 AM, Michael Haenlein wrote: Dear all, I have one dependent variable y and two independent variables x1 and x2 which I would like to use to explain y. x1 and x2 are design factors in an experiment and are not correlated with each other. For example assume that: x1 <- rbind(1,1,1,2,2,2,3,3,3) x2 <- rbind(1,2,3,1,2,3,1,2,3) cor(x1,x2) The problem is that I do not only want to analyze the effect of x1 and x2 on y but also of their interaction x1*x2. Evidently this interaction term has a substantial correlation with both x1 and x2: x3 <- x1*x2 cor(x1,x3) cor(x2,x3) I therefore expect that a simple regression of y on x1, x2 and x1*x2 will lead to biased results due to multicollinearity. For example, even when y is completely random and unrelated to x1 and x2, I obtain a substantial R2 for a simple linear model which includes all three variables. This evidently does not make sense: y <- rnorm(9) model <- lm (y ~ x1 + x2 + x1*x2) summary(model) Is there some function within R or in some separate library that allows me to estimate such a regression without obtaining inconsistent results? Thanks for your help in advance, Michael Michael Haenlein Associate Professor of Marketing ESCP Europe Paris, France [[alternative HTML version deleted]] __ R-help@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@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.
Re: [R] sorting by date
> a <- c( 20071031,20071130, 20071231) b<- sort(as.Date(as.character(a), format="%Y%M%d")) On Aug 2, 2010, at 8:03 PM, Leigh E. Lommen wrote: I am unsure how to sort a column by date if it is currently in the form: MMDD For example the months: 20071031 20071130 20071231 Etc. Regards, Leigh [[alternative HTML version deleted]] __ R-help@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@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.
Re: [R] Dealing with a lot of parameters in a function
use 24 length vectors as parameters instead of numbers e.g. mu=rep(0.5,24) On Mon, Aug 2, 2010 at 11:00 AM, Shentu, Yue wrote: > Hi all, > I'm trying to define and log-likelihood function to work with MLE. > There will be parameters like mu_i, sigma_i, tau_i, ro_i, for i between > 1 to 24. Instead of listing all the parameters, one by one in the > function definition, is there a neat way to do it in R ? The example is > as follows: > > ll<- function(mu1=-0.5,b=1.2,tau_1=0.5,sigma_1=0.5,ro_1=0.7) > { if (tau1>0 && ro<1 && ro>-1) > > -sum(dmnorm(cbind(x,y),c(mu1,b*mu1),matrix(c(tau_1^2,ro_1*tau_1*sigma_1, > ro_1*tau_1*sigma_1,sigma_1^2),nrow=2),log=T)) > else NA > } > > but now I need to have the sum of 24 of these negative log-likelihood. > > Thanks. > > Yue > Notice: This e-mail message, together with any attachme...{{dropped:11}} > > __ > R-help@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. > [[alternative HTML version deleted]] __ R-help@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.
Re: [R] removing spatial auto correlation
Try r-sig-geo Look at spdep, geoR, splancs and sp packages for spatial autocorrelation. Also look at http://cran.r-project.org/web/views/Spatial.html On Mon, Aug 2, 2010 at 3:40 AM, nuncio m wrote: > Hi list, > I am trying to fit arima model for a grid of 360x161x338 points, > where 360x161 is the spatial dimension and 338 is the number of time steps > I > have, which is seasonal. For this purpose I used the auto.arima function > in > forecast package. After fitting residuals at each grid in space, the auto > correlations are still significant ( but < 0.2). This make me think that > the > data could be spatially correlated as well. In such case is it necesary to > remove spatial autocorelations before fitting models in time and are there > some methods available in R to remove the spatial autocorrelations. > Thanks > nuncio > > -- > Nuncio.M > Research Scientist > National Center for Antarctic and Ocean research > Head land Sada > Vasco da Gamma > Goa-403804 > >[[alternative HTML version deleted]] > > __ > R-help@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. > [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Meaning of following function
well "*"(3,2) works but "*"(3,2,3) does not. You should now be able to figure out the logic. It is related to the number of arguments that make sense. Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com On Aug 1, 2010, at 10:56 AM, Ron Michael wrote: Hi friends, I am aware of the function "-"() which acts as minus in ordinary computations. For example: "-"(3, 1) [1] 2 However what is the meaning of "-"(3) [1] -3 I was expecting R to generate some error as it does for "*"(3). What is the logic for that calculation? Thanks, [[alternative HTML version deleted]] __ R-help@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@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.
Re: [R] question!!
?replicate ?apply ?sapply Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com On Aug 1, 2010, at 2:42 AM, leepama wrote: hi!! imade many codes during these days.. I study Statistics please one more question!! ex1<-function(n,p,rho){ muvec1=zeros(1,p) A=eye(p) for(i in 1:p){ for(j in 1:p){ A[i,j]=rho^(abs(i-j)) X=mvrnorm(n,muvec1,A)}} return(X) } this code generates design matrix in linare regression model.. but this code only one data set.. Is there any method to generate several data sets(design Matrix)?? (in R program) please give me help.. -- View this message in context: http://r.789695.n4.nabble.com/question-tp2309277p2309277.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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@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.
Re: [R] Left Outer Join 2 DF's on Multiple Conditions
Look at sqldf package, it is easier to do sql like statements with it. Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com On Jul 25, 2010, at 6:10 PM, harsh yadav wrote: Hi, I am trying to execute the following SQL statement using two data frames: tab1, tab2 : Two Tables Select tab1.*, tab2.*, tab1.tobiiTime - tab2.ruiTime as timeDiff, IFNULL(n-m, -999) as alwaysIncrement FROM tab1 LEFT OUTER JOIN tab2 On tab1.data1 - tab2.mouseX = 0 And tab1.data2 - tab2.mouseY = 0 I am trying to do the following in R:- *#Getting error here:* data <- merge(tab1,tab2, all.x=TRUE, by=(data$data1 - data$mouseX == 0), by=(data$data2 - data$mouseY == 0)) data <- cbind(data, data[, "tobiiTime"] - data[, "ruiTime"], data[, "n"] - data[, "m"]) #Change name of column tobiiTime-ruiTime to timeDiff, for convenience data <- rename(data, c(tobiiTime-ruiTime="timeDiff")) #Change name of column n-m to alwaysIncrement, for convenience data <- rename(data, c(n-m="alwaysIncrement")) *In the merge step, I want to include the following condition of merging:-* tab1.data1 - tab2.mouseX = 0 And tab1.data2 - tab2.mouseY = 0 Any ideas how this could be done. Thanks in advance. Regards, Harsh Yadav [[alternative HTML version deleted]] __ R-help@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@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.
Re: [R] Updating a Data Frame
Looks like your event id is unique. If that is so, why not just do ##Not checked events <- events[sort(events$event),] dataF <- dataF[sort(data$event),] if(doUpdate == 1){ if(!is.null(dataF) && nrow(dataF) > 0){ events[events$eventid %in% dataF$event, c("timestamp", "isSynchronized","timediff)] <- cbind(dataF[,"tobiitime"], rep(1,nrow(dataF), (dataF[i,"ruiTime"]-dataF[,"tobiitime"])) } Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com On Jul 22, 2010, at 2:04 PM, harsh yadav wrote: Hi, I have a global data-frame in my R script. At some point in my script, I want to update certain columns of this data-frame by calling in an update function. The function looks like this: # get events data. This populates a global event data frame in the R- script events <- getEvents(con, eventsFilePath) # events has columns eventid, timeStamp, isSynchronized, timeDiff; with millions of rows updateDB <- function(eventid, newTimeStamp, oldTimeStamp){ timeDiff <- newTimeStamp - oldTimeStamp #Update the events Data Frame events[events$eventid == eventid, "timestamp"] <<- newTimeStamp events[events$eventid == eventid, "isSynchronized"] <<- 1 events[events$eventid == eventid, "timeDiff"] <<- timeDiff } I call this function like: # dataF is a subset of events if(doUpdate == 1){ if(!is.null(dataF) && nrow(dataF) > 0){ len <- nrow(dataF) for(i in 1:len){ updateDB(dataF[i,"eventid"], dataF[i,"tobiiTime"], dataF[i,"ruiTime"]) } } } However, this particular update functionality is performing very slow updates. Is there a better and more efficient way to update multiple fields in a data-frame efficiently. Thanks in advance. Harsh Yadav [[alternative HTML version deleted]] __ R-help@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@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.
Re: [R] replacing elements of distance matrix
My mistake, instead of colnames(d1) use substr(colnames(d1),1,1) or similar On Jul 19, 2010, at 2:15 PM, Nikhil Kaza wrote: Michael, You can modify the following code to suit. Also avoid using dist as a variable name since it is a function in base. However, are you sure you want to do this? Sx is the variance using sites in all the regions! d1 <- apply(x,1, function(i){mahalanobis(x,i,Sx)}) is.na(d1) <- !sapply(id1, grepl, colnames(d1), fixed=T) If on the other hand you want to use only variance within a region modify like this ( i am sure more optimal code can be written) #not tested x.L <- split(x,id1) n.L <- split(rownames(x), id1) for (i in 1:length(x.L)){names(x.L[[i]]) <- n.L[[i]]} m2 <- function(i,j){mahalanobis(j, i, var(j))} m3 <- function(k){apply(as.matrix(k),1,m2,as.matrix(k))} d2 <- lapply(x.L, m3) Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com On Jul 19, 2010, at 11:37 AM, Michael Ralph M. Abrigo wrote: Thanks for the tip, Nikhil. However, i need only one matrix as input for another to compute for non-bipartite matching which minimizes pairwise distances between observations. As such, I need the georeference (id) of the observations for subsequent processing. Below is an illustration. #generate data x <- as.matrix(runif(5)) Sx <- var(x) #generate id set.seed(1) id1 <- sample(1:2,5, replace=T) id2 <- c(1:5) rownames(x) <- paste(id1, id2) #generate distance dist <- as.matrix( + apply(x,1,function(i){ + mahalanobis(x,i,Sx) +} + ) + ) #print matrices x [,1] 1 1 0.2059746 1 2 0.1765568 2 3 0.6870228 2 4 0.3841037 1 5 0.7698414 dist 1 11 22 3 2 41 5 1 1 0. 0.01165534 3.11660015 0.4273402 4.28210082 1 2 0.01165534 0. 3.50943798 0.5801450 4.74056406 2 3 3.11660015 3.50943798 0. 1.2358255 0.09237602 2 4 0.42734018 0.58014499 1.23582554 0.000 2.00395492 1 5 4.28210082 4.74056406 0.09237602 2.0039549 0. The geo-id is composed of two references, the first digit for the region and the next for the observation itself. What I'm thinking of is for pairwise distance between observations of different regions, say site-11 and site-23 or site-24 to be replaced by a large number, say 99. I need the id for future processing, though. Maybe I can stack the matrices generated using your tip to form a block diagonal matrix, but then I do not have my ids? Im really sorry. Im a bit lost. Cheers, Michael On Mon, Jul 19, 2010 at 10:10 PM, Nikhil Kaza wrote: replace dist with mahalanobis distance in the following example. a <- cbind(runif(10), sample(1:3, 10, replace=T)) a.L <- split(a,a[,2]) dist.L <- lapply(a.L, dist) Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com On Jul 19, 2010, at 9:24 AM, Michael Ralph M. Abrigo wrote: Hi! I am trying to implement non-bipartite matching. I have around 500 sites which can be clustered by 10 regions. I am able to calculate pairwise Mahalanobis distances between sites (thanks to another post in the forum). However, I want to constrain my match to sites within the same region. Thus I want to replace elements of the distance matrix with a high value, say 99, for sites not of the same region so that the pair will not be matched. In the original data file I have information on which sites belong to what region. However, when I compute for pairwise Mahalanobis distances, I only use a subset of the file, which, naturally, does not include the georeference of the sites. How should I do this? Any hint will be most appreciated. Btw, I am relatively new in using R. I may export the matrix to another program and replace the elements there, but that is a very very dirty and rough trick that I would rather not do given better options. Many thanks in advance. Cheers, Michael -- "I am most anxious for liberties for our country... but I place as a prior condition the education of the people so that our country may have an individuality of its own and make itself worthy of liberties... " Jose Rizal,1896 [[alternative HTML version deleted]] __ R-help@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. -- "I am most anxious for liberties for our country... but I place as a prior condition the education of the people so that our country may have an individuality of its own and make itself worthy of liberties... " Jose Rizal,1896 -- "I am most anxious for liberties for our country... but I place as a prior condition the education of the people so that our country may have an
Re: [R] replacing elements of distance matrix
Michael, You can modify the following code to suit. Also avoid using dist as a variable name since it is a function in base. However, are you sure you want to do this? Sx is the variance using sites in all the regions! d1 <- apply(x,1, function(i){mahalanobis(x,i,Sx)}) is.na(d1) <- !sapply(id1, grepl, colnames(d1), fixed=T) If on the other hand you want to use only variance within a region modify like this ( i am sure more optimal code can be written) #not tested x.L <- split(x,id1) n.L <- split(rownames(x), id1) for (i in 1:length(x.L)){names(x.L[[i]]) <- n.L[[i]]} m2 <- function(i,j){mahalanobis(j, i, var(j))} m3 <- function(k){apply(as.matrix(k),1,m2,as.matrix(k))} d2 <- lapply(x.L, m3) Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com On Jul 19, 2010, at 11:37 AM, Michael Ralph M. Abrigo wrote: Thanks for the tip, Nikhil. However, i need only one matrix as input for another to compute for non-bipartite matching which minimizes pairwise distances between observations. As such, I need the georeference (id) of the observations for subsequent processing. Below is an illustration. #generate data x <- as.matrix(runif(5)) Sx <- var(x) #generate id set.seed(1) id1 <- sample(1:2,5, replace=T) id2 <- c(1:5) rownames(x) <- paste(id1, id2) #generate distance dist <- as.matrix( + apply(x,1,function(i){ + mahalanobis(x,i,Sx) +} + ) + ) #print matrices x [,1] 1 1 0.2059746 1 2 0.1765568 2 3 0.6870228 2 4 0.3841037 1 5 0.7698414 dist 1 11 22 3 2 41 5 1 1 0. 0.01165534 3.11660015 0.4273402 4.28210082 1 2 0.01165534 0. 3.50943798 0.5801450 4.74056406 2 3 3.11660015 3.50943798 0. 1.2358255 0.09237602 2 4 0.42734018 0.58014499 1.23582554 0.000 2.00395492 1 5 4.28210082 4.74056406 0.09237602 2.0039549 0. The geo-id is composed of two references, the first digit for the region and the next for the observation itself. What I'm thinking of is for pairwise distance between observations of different regions, say site-11 and site-23 or site-24 to be replaced by a large number, say 99. I need the id for future processing, though. Maybe I can stack the matrices generated using your tip to form a block diagonal matrix, but then I do not have my ids? Im really sorry. Im a bit lost. Cheers, Michael On Mon, Jul 19, 2010 at 10:10 PM, Nikhil Kaza wrote: replace dist with mahalanobis distance in the following example. a <- cbind(runif(10), sample(1:3, 10, replace=T)) a.L <- split(a,a[,2]) dist.L <- lapply(a.L, dist) Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com On Jul 19, 2010, at 9:24 AM, Michael Ralph M. Abrigo wrote: Hi! I am trying to implement non-bipartite matching. I have around 500 sites which can be clustered by 10 regions. I am able to calculate pairwise Mahalanobis distances between sites (thanks to another post in the forum). However, I want to constrain my match to sites within the same region. Thus I want to replace elements of the distance matrix with a high value, say 99, for sites not of the same region so that the pair will not be matched. In the original data file I have information on which sites belong to what region. However, when I compute for pairwise Mahalanobis distances, I only use a subset of the file, which, naturally, does not include the georeference of the sites. How should I do this? Any hint will be most appreciated. Btw, I am relatively new in using R. I may export the matrix to another program and replace the elements there, but that is a very very dirty and rough trick that I would rather not do given better options. Many thanks in advance. Cheers, Michael -- "I am most anxious for liberties for our country... but I place as a prior condition the education of the people so that our country may have an individuality of its own and make itself worthy of liberties... " Jose Rizal,1896 [[alternative HTML version deleted]] __ R-help@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. -- "I am most anxious for liberties for our country... but I place as a prior condition the education of the people so that our country may have an individuality of its own and make itself worthy of liberties... " Jose Rizal,1896 -- "I am most anxious for liberties for our country... but I place as a prior condition the education of the people so that our country may have an individuality of its own and make itself worthy of liberties... " Jose Rizal,1896 __ R-help@r-project.or
Re: [R] replacing elements of distance matrix
replace dist with mahalanobis distance in the following example. a <- cbind(runif(10), sample(1:3, 10, replace=T)) a.L <- split(a,a[,2]) dist.L <- lapply(a.L, dist) Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com On Jul 19, 2010, at 9:24 AM, Michael Ralph M. Abrigo wrote: Hi! I am trying to implement non-bipartite matching. I have around 500 sites which can be clustered by 10 regions. I am able to calculate pairwise Mahalanobis distances between sites (thanks to another post in the forum). However, I want to constrain my match to sites within the same region. Thus I want to replace elements of the distance matrix with a high value, say 99, for sites not of the same region so that the pair will not be matched. In the original data file I have information on which sites belong to what region. However, when I compute for pairwise Mahalanobis distances, I only use a subset of the file, which, naturally, does not include the georeference of the sites. How should I do this? Any hint will be most appreciated. Btw, I am relatively new in using R. I may export the matrix to another program and replace the elements there, but that is a very very dirty and rough trick that I would rather not do given better options. Many thanks in advance. Cheers, Michael -- "I am most anxious for liberties for our country... but I place as a prior condition the education of the people so that our country may have an individuality of its own and make itself worthy of liberties... " Jose Rizal,1896 [[alternative HTML version deleted]] __ R-help@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@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.
Re: [R] Help with replacing a substring in a string
gsub(".", " ", "abc.degg.hijk", fixed=T) On Jul 19, 2010, at 7:37 AM, Tolga I Uzuner wrote: Actually, I think I got it, need to use gsub. From: Tolga I Uzuner Sent: 19 July 2010 12:11 To: 'r-help@r-project.org' Subject: Help with replacing a substring in a string Dear R Users, I am trying to replace a substring in a string with something else. For example: if we have "abc.degg.hijk" I would like to replace all the "." with a SPACE to become "abc degg hijk" I have tried the replace.substring.wild function in the Hmisc package but get this error: replace.substring.wild("abc.degg.hijk","*.*"," ") Error in replace.substring.wild("abc.degg.hijk", "*.*", " ") : does not handle > 1 * in old Can't really figure out from the function description or the error message what I am doing wrong. Any assistance would be appreciated, thanks in advance. Regards, Tolga This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. [[alternative HTML version deleted]] __ R-help@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@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.
Re: [R] Two Dimensional Transformation
Unless I am missing something this should do it a<- cbind(runif(10),runif(10)) b <- cbind(a[,1]+a[,2], a[,1]/a[,2]) On Jul 16, 2010, at 7:00 AM, Ravi Ramaswamy wrote: Hi - I am trying to map a two dimensional area A to another two dimensional area B using a function. For instance A = {(x,y), 0https://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@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.
Re: [R] Cut a within elements by length, not value, of vectors
Building on Erik's solution and because it would easier to do date arithmetic.. d1 <- as.character(date) d1 <- ifelse(nchar(d1)<4, paste(0,d1,sep=""),d1) d2 <- as.Date(date, "%m%d") On Jul 15, 2010, at 1:21 PM, btc1 wrote: Hello, I have a vector, "dates", as a series of 3 digit elements, i.e. > date [1] 528 528 528 528 528 528 528 528 528 528 528 528 708 708 708 708 708 708 [19] 708 708 708 708 529 529 529 529 529 529 529 529 529 529 529 529 529 529 [37] 529 624 I need to convert them into julian, but have to insert a "/" or "-" after the first number within each element of the vector (5/28 5/28 etc). Found plenty functions to replace by a pattern but not to cut by a certain number of digits with an element. Alternately, if I could run all the elements into one long vector and then cut every one then two digits, that would work as well. Thanks. -- View this message in context: http://r.789695.n4.nabble.com/Cut-a-within-elements-by-length-not-value-of-vectors-tp2290440p2290440.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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@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.
Re: [R] Batch file export
write.csv(z, paste("c:/z_",i,".csv"sep='')) You will have to modify this to prepend 0s. Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com On Jul 13, 2010, at 10:03 AM, Michael Haenlein wrote: Dear all, I have a code that generates data vectors within R. For example assume: z <- rlnorm(1000, meanlog = 0, sdlog = 1) Every time a vector has been generated I would like to export it into a csv file. So my idea is something as follows: for (i in 1:100) { z <- rlnorm(1000, meanlog = 0, sdlog = 1) write.csv(z, "c:/z_i.csv") Where "z_i.csv" is a filename that is related to the run (e.g. z_001.csv, z_002.csv, ...). Could anyone please advice me on the most convenient way of doing this? Thanks very much in advance, Michael [[alternative HTML version deleted]] __ R-help@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@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.
Re: [R] Substring function?
well %in% is really checking if the element is in the set and is not a substring operator. To get the result you want, try content[grepl(search$signatures, content$urls),] For multiple operations you could try sapply(search$signatures, grepl, x=content$urls) Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com On Jul 13, 2010, at 8:22 AM, Ralf B wrote: Hi all, I would like to detect all strings in the vector 'content' that contain the strings from the vector 'search'. Here a code example: content <- data.frame(urls=c( "http://www.google.com/search?source=ig&hl=en&rlz=&=&q=stuff&aq=f&aqi=g10&aql=&oq=&gs_rfai=CrrIS3 ", "http://search.yahoo.com/search;_ylt=Atvki9MVpnxuEcPmXLEWgMqbvZx4?p=stuff&toggle=1 ") ) search <- data.frame(signatures=c("http://www.google.com/search";)) subset(content, search$signatures %in% content$urls) I am getting an error: [1] urls <0 rows> (or 0-length row.names) What I would like to achieve is the return of "http://www.google.com/search?source=ig&hl=en&rlz=&=&q=stuff&aq=f&aqi=g10&aql=&oq=&gs_rfai=CrrIS3 ". Is that possible? In practice I would like to run this over 1000s of strings in 'content' and 100s of strings in 'search'. Could I run into performance issues with this approach and, if so, are there better ways? Best, Ralf __ R-help@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@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.
Re: [R] Batch files process and String parsing
?list.files In particular look at pattern argument. ?file.rename ?lapply ?read.table ?"[" Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com On Jul 7, 2010, at 1:11 PM, jd6688 wrote: Here are what i am going to accomplish: I have 400 files named as xxx.txt. the content of the file looks like the following: namecount 1. aaa 100 2. bbb2000 3. ccc300 4. ddd 3000 more that 1000 rows in each files. these are the areas i need help: 1. how can i only read in the files with the string patterns ggg or fff as part of the file names? for instance, I only need the file names with the ggg or fff in it x_ggg_y_1.txt _fff__xxx.txt i don't need to read in the files, such as _aaa_.txt 2.how cam rename the files: for instance: x_ggg_y_1.txt==>changed to ggg1a.txt 3.after the files read in, how can i only keep the rows with the aaa and bbb, everything elses show be removed from the files, but the files still remain the same file name? for instance, in the x_ggg_y_1.txt file, it shouls looks like: namecount 1. aaa100 2. bbb2000 3. aaa300 4. bbb400 Thanks so lot, I am very new to R, I am looking forward to any helps from you. -- View this message in context: http://r.789695.n4.nabble.com/Batch-files-process-and-String-parsing-tp2281208p2281208.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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@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.
Re: [R] how to display the clock time in the loop
explicit call to print usually works for me. library(audio) for (i in 1:5){ wait(60) print(Sys.time()) } On Jul 1, 2010, at 4:30 PM, Matt Shotwell wrote: > Try to flush output after printing: > > cat(paste(Sys.time()),"\n"); flush(stdout()) > > On Thu, 2010-07-01 at 16:17 -0400, Jack Luo wrote: >> Hi, >> >> I am doing some computation which is pretty time consuming, I want >> R to >> display CPU time after each iteration using the command Sys.time(). >> However, >> I found that the code only began to display the CPU time after >> quite a while >> and several iterations have finished. Is there a way to ask R to >> display >> time right after each iteration is finished? >> >> Thanks, >> >> -Jun >> >> [[alternative HTML version deleted]] >> >> __ >> R-help@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. > -- > Matthew S. Shotwell > Graduate Student > Division of Biostatistics and Epidemiology > Medical University of South Carolina > http://biostatmatt.com > > __ > R-help@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. [[alternative HTML version deleted]] __ R-help@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.
Re: [R] How to delete rows based on replicate values in one column with some extra calcuation
You can do this in reshape package as mentioned earlier. However, if you need a solution with aggregate here it is a <- with(data, aggregate(cbind(v1,v2), by=list(x,y,z),sum)) names (a) <- c("x","y","z","v1","v2") Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com On Jun 29, 2010, at 7:56 PM, Yi wrote: > Great help. It works when the first and the second columns are > ordered the same way. But aggregate does not work for the following > case: > z=c('ab','ah','bc','ah','dv') > x=substr(z,start=1,stop=1) > y=substr(z,start=2,stop=2) > v1=5:9 > v2=7:11 > data=data.frame(x,y,z,v1,v2) > > data > x y z v1 v2 > 1 a b ab 5 7 > 2 a h ah 6 8 > 3 b c bc 7 9 > 4 a h ah 8 10 > 5 d v dv 9 11 > > ##I want to do the aggregate WRT z and sum up v1 and v2. The > expected output is: > >x y z v1 v2 > 1 a b ab 5 7 > 2 a h ah 14 18 > 3 b c bc 7 9 > 4 d v dv 9 11 > > ### I do this almost manually. As you see here: > > newdata=aggregate(data$v1,by=list(data$z),sum) > newdata2=aggregate(data$v2,by=list(data$z),sum) > x=substr(newdata$Group.1,start=1,stop=1) > y=substr(newdata$Group.1,start=2,stop=2) > data.frame(x,y,newdata$Group.1,newdata$x,newdata2$x) > new=data.frame(x,y,newdata$Group.1,newdata$x,newdata2$x) > names(new)=c('x','y','z','v1','v2') > new > > Because I do not think 'aggregate' can not set z as a list and at > the same time keep x and y for z. > > Any tips? I mean my way is too 'silly'. > > Thanks all in advance! > > Yi > > On Mon, Jun 28, 2010 at 7:58 PM, Nikhil Kaza > wrote: > > aggregate(data$third, by=list(data$first), sum) > > or > > reqiure(reshape) > cast(melt(data), ~first, sum) > > > > On Jun 28, 2010, at 9:30 PM, Yi wrote: > > > first=c('u','b','e','k','j','c','u','f','c','e') > second > = > c > ('usa > ','Brazil > ','England','Korea','Japan','China','usa','France','China','England') > third=1:10 > data=data.frame(first,second,third) > > [[alternative HTML version deleted]] __ R-help@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.
Re: [R] How to delete the replicate rows by summing up the numeric columns
require(reshape) cast(data, first+second~ ., sum) Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com On Jun 29, 2010, at 3:05 PM, Yi wrote: first=c('u','b','e','k','j','c','u','f','c','e') second = c ('usa ','Brazil ','England','Korea','Japan','China','usa','France','China','England') third=1:10 data=data.frame(first,second,third) __ R-help@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.
Re: [R] How to delete rows based on replicate values in one column with some extra calcuation
aggregate(data$third, by=list(data$first), sum) or reqiure(reshape) cast(melt(data), ~first, sum) On Jun 28, 2010, at 9:30 PM, Yi wrote: first=c('u','b','e','k','j','c','u','f','c','e') second = c ('usa ','Brazil ','England','Korea','Japan','China','usa','France','China','England') third=1:10 data=data.frame(first,second,third) __ R-help@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.
Re: [R] optim() not finding optimal values
Your function is very irregular, so the optim is likely to return local minima rather than global minima. Try different methods (SANN, CG, BFGS) and see if you get the result you need. As with all numerical optimsation, I would check the sensitivity of the results to starting values. Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com On Jun 26, 2010, at 4:27 PM, Derek Ogle wrote: I am trying to use optim() to minimize a sum-of-squared deviations function based upon four parameters. The basic function is defined as ... SPsse <- function(par,B,CPE,SSE.only=TRUE) { n <- length(B) # get number of years of data B0 <- par["B0"]# isolate B0 parameter K <- par["K"] # isolate K parameter q <- par["q"] # isolate q parameter r <- par["r"] # isolate r parameter predB <- numeric(n) predB[1] <- B0 for (i in 2:n) predB[i] <- predB[i-1]+r*predB[i-1]*(1-predB[i-1]/K)- B[i-1] predCPE <- q*predB sse <- sum((CPE-predCPE)^2) if (SSE.only) sse else list(sse=sse,predB=predB,predCPE=predCPE) } My call to optim() looks like this # the data d <- data.frame(catch= c (9,113300,155860,181128,198584,198395,139040,109969,71896,59314,62300,65343,76990,88606,118016,108250,108674 ), cpe = c (109.1,112.4,110.5,99.1,84.5,95.7,74.1,70.2,63.1,66.4,60.5,89.9,117.0,93.0,116.6,90.0,105.1 )) pars <- c(80,100,0.0001,0.17) # put all parameters into one vector names(pars) <- c("B0","K","q","r") # name the parameters ( SPoptim <- optim(pars,SPsse,B=d$catch,CPE=d$cpe) )# run optim() This produces parameter estimates, however, that are not at the minimum value of the SPsse function. For example, these parameter estimates produce a smaller SPsse, parsbox <- c(732506,1160771,0.0001484,0.4049) names(parsbox) <- c("B0","K","q","r") ( res2 <- SPsse(parsbox,d$catch,d$cpe,SSE.only=FALSE) ) Setting the starting values near the parameters shown in parsbox even resulted in a movement away from (to a larger SSE) those parameter values. ( SPoptim2 <- optim(parsbox,SPsse,B=d$catch,CPE=d$cpe) )# run optim() This "issue" most likely has to do with my lack of understanding of optimization routines but I'm thinking that it may have to do with the optimization method used, tolerance levels in the optim algorithm, or the shape of the surface being minimized. Ultimately I was hoping to provide an alternative method to fisheries biologists who use Excel's solver routine. If anyone can offer any help or insight into my problem here I would be greatly appreciative. Thank you in advance. __ R-help@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@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.
Re: [R] Spatial: number of independent components?
I just updated spdep and I see that as.spam.listw works. Below is sessionInfo Furthermore, it may be straightforward to condense the adjacency matrix *before* converting to graph which may help a little bit. You can profile the code and see which part needs speeding up. library(spdep) library(igraph) x=matrix(c(0,1,0,0,0, 0,1,1,0,0, 0,0,0,0,0, 0,0,0,1,0, 0,0,0,1,0),nrow=5) a <- as.spam.listw(nb2listw(cell2nb(nrow(x),ncol(x),torus=T), style="B")) ind <- which(x>0) b <- a[ind, ind] g1 <- graph.adjacency(b) clusters(g1)$no --- R version 2.11.1 (2010-05-31) i386-apple-darwin9.8.0 locale: [1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] stats graphics grDevices utils [5] datasets methods base other attached packages: [1] igraph_0.5.3 spam_0.22-0 [3] spdep_0.5-11 coda_0.13-5 [5] deldir_0.0-12 maptools_0.7-34 [7] foreign_0.8-40 nlme_3.1-96 [9] MASS_7.3-6 Matrix_0.999375-39 [11] lattice_0.18-8 boot_1.2-42 [13] sp_0.9-64 loaded via a namespace (and not attached): [1] grid_2.11.1 tools_2.11.1 Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com __ R-help@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.
Re: [R] Spatial: number of independent components?
I have spdep 4.58. Perhaps it is deprecated in the new version. Try looking for sparse matrix representation in the help files for spdep Nikhil On Mon, Jun 21, 2010 at 6:10 AM, Daniel Malter wrote: > > as.spam.listw is an unknown function. Is it in a different package? > > Daniel > > other attached packages: > [1] spdep_0.5-11coda_0.13-5 deldir_0.0-12 > maptools_0.7-34 foreign_0.8-38 nlme_3.1-96 MASS_7.3-3 > [8] Matrix_0.999375-31 lattice_0.17-26 boot_1.2-41 sp_0.9-64 > igraph_0.5.3RandomFields_1.3.41 svSocket_0.9-48 > [15] TinnR_1.0.3 R2HTML_1.59-1 Hmisc_3.7-0 > survival_2.35-7 > > loaded via a namespace (and not attached): > [1] cluster_1.12.1 grid_2.10.0svMisc_0.9-56 tools_2.10.0 > > -- > View this message in context: > http://r.789695.n4.nabble.com/Spatial-number-of-independent-components-tp2262018p2262422.html > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@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. > [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Spatial: number of independent components?
Instead of nb2mat try as.spam.listw(nb2listw(cell2nb(...))) this will coerce the adjacency matrix into a sparse matrix representation saving lot of memory. Nikhil On Sun, Jun 20, 2010 at 10:27 PM, Daniel Malter wrote: > > Hi, thanks much. This works in principle. The corrected code is below: > > a <- nb2mat(cell2nb(nrow(x),ncol(x),torus=T), style="B") > g <- delete.vertices(graph.adjacency(a), which(x!=1)-1) > > plot(g) > clusters(g) > > the $no argument of the clusters(g) function is the sought after number. > However, the function is very slow, and my machine runs out of memory (1G) > for a 101 by 101 matrix. > > Daniel > -- > View this message in context: > http://r.789695.n4.nabble.com/Spatial-number-of-independent-components-tp2262018p2262090.html > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@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. > [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Spatial: number of independent components?
I am sure this can be written in a much more elegant and faster code. One way I can think of, is to modify cell2nb code and create a new function that creates the neighbour lists of only cells that are not 0. These are best directed to R-sig-Geo list. However, the following might work. library(spdep) library(igraph) x=matrix(c(0,1,0,0,0, 0,1,1,0,0, 0,0,0,0,0, 0,0,0,1,0, 0,0,0,1,0),nrow=5) a <- nb2mat(cell2nb(nrow(x),ncol(x)), style="B", torus="TRUE") g <- delete.vertices(graph.adjacency(a), which(x!=1)-1) plot(g) clusters(g) Nikhil --- Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com On Jun 20, 2010, at 7:17 PM, Daniel Malter wrote: Hi all, I am sorry if this is a very basic quesion, but I have no experience with analyzing spatial data and could not find the right function/ package quickly. Any hints would be much appreciated. I have a matrix of spatial point patterns like the one below and want to find the number of independent components (if that's the right term) in that matrix (or in that image). x=matrix(c(0,1,0,0,0, 0,1,1,0,0, 0,0,0,0,0, 0,0,0,1,0, 0,0,0,1,0),nrow=5) image(x) I can find the number of populated points easily table(x) #or more generally sum(x!=0) But I want to find the number of independent components. The answer in this example should be 2. There are three criteria to the function I am seeking: 1. Points that have a neighboring nonzero point should be counted as one contiguous component. 2. The function should respect that the matrix is projected on a torso. That is, points in the leftmost column border points in the rightmost column and points in the top row border points in the bottom row (if they are contiguous when you wrap the image around a cylinder). 3. The function should be fast/efficient since I need to run this over thousands of images/matrices. Is anyone aware of an implementation of such a function? Thanks much for your help, Daniel -- View this message in context: http://r.789695.n4.nabble.com/Spatial-number-of-independent-components-tp2262018p2262018.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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@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.
Re: [R] Find the 50 highest values in a matrix
Matrix is just a vector. So order should work haven't verified the following code. a <- matrix(rnorm(4000*2000), 4000, 2000) b <- order(a, na.last=TRUE, decreasing=TRUE)[1:50] use %% or %/% to get the row# and column #s Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com On Jun 18, 2010, at 1:41 AM, uschlecht wrote: Hi, I have a huge matrix (4000 * 2000 data points) and I would like to retrieve the coordinates (column and row) for the top 50 (or x) values. Some positions in the matrix have NA as a value. These should be discarded. My current method is to replace all NAs by 0, then rank all the values and then extract the positions with the 50 highest ranks. It is very time-consuming! Is there a simpler way to do this? Thank you, Ulrich -- View this message in context: http://r.789695.n4.nabble.com/Find-the-50-highest-values-in-a-matrix-tp2259721p2259721.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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. b - b%%nrow(a) __ R-help@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.
Re: [R] Backslash in paste() function
On Jun 16, 2010, at 9:23 AM, Stefan Petersson wrote: Just double all the backslashes and you are fine. In order to see the outcome, use cat() (not print). Uwe Ligges On 16.06.2010 09:49, Stefan Petersson wrote: Hi, I'm trying to build a vector of latex commands. However, I need the command strings to begin with a backslash "\". I have: test<- c('foo','bar') and I need to rebuild the array, encapsulating the text items with latex stuff, like this: paste("\parbox[b]{3cm}{", test, "}", fill=TRUE) Actually, cat() prints the string fine if one uses double backslashes. However, I can't save the result from cat() back to a vector. And when I use cat() inside the latex function from the Hmisc package, I only get errors... The (not so) funny thing is that I can use paste() on a few latex commands. So if I needed to make the strings bold, the following works: paste("\bfseries{", test, "}") I've read the R FAQ 7.37, but it only deals with cat(). Not paste. Or I just didn't get it :o/ Is there a way to paste() the backslash character? sessionInfo() R version 2.11.1 (2010-05-31) i486-pc-linux-gnu locale: [1] LC_CTYPE=en_US.utf8 LC_NUMERIC=C [3] LC_TIME=en_US.utf8LC_COLLATE=en_US.utf8 [5] LC_MONETARY=C LC_MESSAGES=en_US.utf8 [7] LC_PAPER=en_US.utf8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.utf8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base __ R-help 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. Sorry... no love. Double backslashes doesn't cut it. Double backslashes prints nothing. Single backslashes removes the first character of my command. But no single backslash! paste("\parbox[b]{3cm}{", test, "}", fill=TRUE) Gives me: [1] arbox[b]{3cm}{ foo } [2] arbox[b]{3cm}{ bar } and paste("\\parbox[b]{3cm}{", test, "}", fill=TRUE) Gives me: [1] parbox[b]{3cm}{ foo } [2] parbox[b]{3cm}{ bar } No backslash. I suppose what Uwe was saying is that you should use a <- paste("\\parbox[b]{3cm}{", test, "}", fill=TRUE) cat(a, file="out.tex") __ R-help@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@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.
Re: [R] [R-sig-Geo] How to extract coordinates values from a shapefile?
What does coordinates() give for a polygon? centeroid? I suppose I could look up the code, but it would be immensely helpful if the help page could specify it. regards Nikhil On Jun 9, 2010, at 3:50 PM, Rodrigo Aluizio wrote: > I'm not sure if this is what you want. But the function > coordinates() in sp package gives you the coordinates of > SpatialObjects. > > Regards. > > Rodrigo. > > 2010/6/9 Nikhil Kaza > You need to execute gpclibPermit() to enable gpclib. > > library(maptools) should have issued a warning to that effect. > > > Nikhil Kaza > Asst. Professor, > City and Regional Planning > University of North Carolina > > nikhil.l...@gmail.com > > On Jun 9, 2010, at 3:34 PM, Thiago Veloso wrote: > > > Dear Nikhil, > > > > Thank you for your input. I tried your suggestion and received the > > following error: > > > > > fortify(br) > > Using UF_05 to define regions. > > Error: isTRUE(gpclibPermitStatus()) is not TRUE > > > > > > > Do you know what else could be done? > > > > Best wishes, > > > > Thiago. > > > > --- On Wed, 9/6/10, Nikhil Kaza wrote: > > > > From: Nikhil Kaza > > Subject: Re: [R-sig-Geo] How to extract coordinates values from a > > shapefile? > > To: "Thiago Veloso" > > Cc: r-sig-...@stat.math.ethz.ch > > Date: Wednesday, 9 June, 2010, 16:00 > > > > Look at this. Not sure if this is indeed what you want or if you > > actually have to unproject them. > > > > http://www.mail-archive.com/r-sig-...@stat.math.ethz.ch/ > msg04715.html > > > > require(ggplot2) > > fortify(br) > > > > > > Nikhil Kaza > > Asst. Professor, > > City and Regional Planning > > University of North Carolina > > > > nikhil.l...@gmail.com > > > > On Jun 9, 2010, at 2:45 PM, Thiago Veloso wrote: > > > > > Dear R colleagues, > > > Does anyone know if it's possible to create a vector with > > coordinate values extracted from a shape loaded with readShapePoly > > command of "maptools" package? > > > For example, the following code loads the shapefile containing > > Brazilian states division: > > > library(maptools)br<-readShapePoly("BR-map/3.shp") > > > What I need to do is create a vector which contains all lat and > > lon values inside this shapefile... > > > Thanks in advance, > > > Thiago Veloso. > > > > > > > > > > > > [[alternative HTML version deleted]] > > > > > > ___ > > > R-sig-Geo mailing list > > > r-sig-...@stat.math.ethz.ch > > > https://stat.ethz.ch/mailman/listinfo/r-sig-geo > > > > > > >[[alternative HTML version deleted]] > > ___ > R-sig-Geo mailing list > r-sig-...@stat.math.ethz.ch > https://stat.ethz.ch/mailman/listinfo/r-sig-geo > [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Odp: strange issue with "which" on "seq"
which(abs(v - .1) <= .Machine$double.eps) seems to me too cumbersome to write. Any other easier way? all.equal does not quite work Nikhil On Jun 9, 2010, at 7:54 AM, Petr PIKAL wrote: Hi r-help-boun...@r-project.org napsal dne 09.06.2010 13:16:40: Dear R community, I am puzzled by the following lines: v <- seq(-0.5,0.5,by=0.1) v [1] -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 which(v == -0.4) [1] 2 which(v == 0) [1] 6 which(v == 0.1) integer(0) which(v == 0.2) integer(0) which(v == 0.3) integer(0) which(v == 0.4) [1] 10 which(v == 0.5) [1] 11 Why "which" can only match some of the values in "v"? Are the numbers generated by "seq" not exact fractional numbers? Please, help me to understand this. Well FAQ 7.31 was not here some time. Computing in binary results in finite precision of fractional numbers. v <- seq(-0.5,0.5,by=0.1) v[7]-0.1 [1] 8.326673e-17 Regards Petr J Dr James Foadi PhD Membrane Protein Laboratory (MPL) Diamond Light Source Ltd Diamond House Harewell Science and Innovation Campus Chilton, Didcot Oxfordshire OX11 0DE Email: james.fo...@diamond.ac.uk Alt Email: j.fo...@imperial.ac.uk -- This e-mail and any attachments may contain confidential... {{dropped:8}} __ R-help@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@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@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.
Re: [R] reformat time from hhmm
Minor correction below. Use 0 instead of space if you are using %H On Thu, Jun 3, 2010 at 8:55 PM, nikhil kaza wrote: > ?ifelse > > > t2 <- ifelse(nchar(times)<4, paste("0", times, sep=""), times) > > > strptime(t2, "%H%M") > > Nikhil > > > > On Thu, Jun 3, 2010 at 5:21 PM, Peter Moore wrote: > >> Hi, >> I'm newish to R, a recent convert from Matlab... So far I'm impressed, and >> determined to solve the following problem, which seems like it should be >> easy: >> I have a long (millions of points) data series recorded with a datalogger >> that produced a timestamp in 4 columns: Year, Day of Year, Time in (H)HMM >> and Seconds. I would like to have R interpret these columns as a time >> object and have made some progress (e.g., using paste() to create a single >> column and then strptime() to interpret -- is that too roundabout??), but >> one thing is throwing me off and I can't seem to conquer it. The >> hour-minute column in the raw data has no colon, so noon looks like >> "1200". >> Morning times have only 3 characters and afternoon times have 4. I've >> been >> playing around with a fake set of times: >> times <- c(110, 230, 459, 1001, 1238, 1922) >> >> When I use >> strptime(data, "%k%M") >> the last three are interpreted fine but the first three are messed up >> because, for some reason, (even though I use %k for hour format?) the >> first >> two characters are assumed to be hour and the remaining one is minutes. >> For >> times[3] I get NA because R doesn't know what to do with 45 hours... >> [1] "2010-06-03 11:00:00" "2010-06-03 23:00:00" NA >> [4] "2010-06-03 10:01:00" "2010-06-03 12:38:00" "2010-06-03 19:22:00" >> >> Fair enough, so I tried a different angle, using an if...else statement: >> hours <- if(nchar(times)>3) strtrim(times,2) else strtrim(times,1) >> >> This worked great when times was only a vector of length=1, but when I try >> to apply it to something larger, I get the following warning: >> Warning message: >> In if(nchar(times)>3) strtrim(times,2) else strtrim(times,1) : >> the condition has length > 1 and only the first element will be used >> and the output hours are only the first character. Not entirely sure if I >> understand this. >> >> Any advice on how to do this? Are there packages or commands that I'm not >> aware of that know how to deal with (h)hmm times? >> >> Thanks much, >> -Pete >> - >> platform i486-pc-linux-gnu >> arch i486 >> os linux-gnu >> system i486, linux-gnu >> status >> major 2 >> minor 10.1 >> year 2009 >> month 12 >> day14 >> svn rev50720 >> language R >> version.string R version 2.10.1 (2009-12-14) >> >> -- >> Pete Moore >> Postdoctoral Research Associate >> Dept. Geological & Atmospheric Sciences >> Iowa State University >> >>[[alternative HTML version deleted]] >> >> __ >> R-help@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. >> > > [[alternative HTML version deleted]] __ R-help@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.
Re: [R] reformat time from hhmm
?ifelse > t2 <- ifelse(nchar(times)<4, paste(" ", times, sep=""), times) > strptime(t2, "%H%M") Nikhil On Thu, Jun 3, 2010 at 5:21 PM, Peter Moore wrote: > Hi, > I'm newish to R, a recent convert from Matlab... So far I'm impressed, and > determined to solve the following problem, which seems like it should be > easy: > I have a long (millions of points) data series recorded with a datalogger > that produced a timestamp in 4 columns: Year, Day of Year, Time in (H)HMM > and Seconds. I would like to have R interpret these columns as a time > object and have made some progress (e.g., using paste() to create a single > column and then strptime() to interpret -- is that too roundabout??), but > one thing is throwing me off and I can't seem to conquer it. The > hour-minute column in the raw data has no colon, so noon looks like "1200". > Morning times have only 3 characters and afternoon times have 4. I've been > playing around with a fake set of times: > times <- c(110, 230, 459, 1001, 1238, 1922) > > When I use > strptime(data, "%k%M") > the last three are interpreted fine but the first three are messed up > because, for some reason, (even though I use %k for hour format?) the first > two characters are assumed to be hour and the remaining one is minutes. > For > times[3] I get NA because R doesn't know what to do with 45 hours... > [1] "2010-06-03 11:00:00" "2010-06-03 23:00:00" NA > [4] "2010-06-03 10:01:00" "2010-06-03 12:38:00" "2010-06-03 19:22:00" > > Fair enough, so I tried a different angle, using an if...else statement: > hours <- if(nchar(times)>3) strtrim(times,2) else strtrim(times,1) > > This worked great when times was only a vector of length=1, but when I try > to apply it to something larger, I get the following warning: > Warning message: > In if(nchar(times)>3) strtrim(times,2) else strtrim(times,1) : > the condition has length > 1 and only the first element will be used > and the output hours are only the first character. Not entirely sure if I > understand this. > > Any advice on how to do this? Are there packages or commands that I'm not > aware of that know how to deal with (h)hmm times? > > Thanks much, > -Pete > - > platform i486-pc-linux-gnu > arch i486 > os linux-gnu > system i486, linux-gnu > status > major 2 > minor 10.1 > year 2009 > month 12 > day14 > svn rev50720 > language R > version.string R version 2.10.1 (2009-12-14) > > -- > Pete Moore > Postdoctoral Research Associate > Dept. Geological & Atmospheric Sciences > Iowa State University > >[[alternative HTML version deleted]] > > __ > R-help@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. > [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Faster union of polygons?
Reduce might work. Not sure about the speed advantages though. It does simplify code. Unionall <- function(x) Reduce('union', x) leaveout <- Unionall(leaves) On Tue, Jun 1, 2010 at 9:53 PM, Remko Duursma wrote: > Dear R-helpers, > > thanks for yesterday's speeding-up tip. Here is my next query: > > I have lots of polygons (not necessarily convex ones, and they never > have holes) given by x,y coordinates. > > I want to get the polygon that is the union of these polygons. This is > my current method, but I am hoping there is a faster method (up to > thousands of polygons, each with ca. 40 xy points). > > Example: > > library(gpclib) > > # A polygon > leaf <- structure(c(0, 1, 12.9, 16.5, 18.8, 17, 16.8, 15.5, 12.1, 8.2, > 6.3, 5, 2, 0, -1.5, -4.3, -6.6, -10.3, -14.8, -19.4, -22.2, -23.5, > -22.2, -17.6, -7.8, 0, 0, -2.4, 2.8, 8.9, 19.9, 33.9, 34.8, 40.4, > 49.7, 69.2, 77.4, 83.4, 91.4, 99, 92.8, 87.3, 81.2, 71.1, 57.6, > 45.4, 39.2, 26, 15.6, 5.3, 0.6, 0), .Dim = c(26L, 2L), .Dimnames = list( >NULL, c("X", "Y"))) > > # Lots of polygons: > releaf <- > function(leaf)cbind(leaf[,1]+rnorm(1,0,50),leaf[,2]+rnorm(1,0,50)) > leaves <- replicate(500, releaf(leaf), simplify=FALSE) > > # Make into gpc.poly class: > leaves <- lapply(leaves, as, "gpc.poly") > > # Make union . > system.time({ > leavesoutline <- union(leaves[[1]], leaves[[2]]) > for(i in 3:length(leaves))leavesoutline <- union(leavesoutline, > leaves[[i]]) > }) > # about 1sec here. > > # Check it: > plot(leavesoutline) > > > > thanks! > > Remko > > > - > Remko Duursma > Research Lecturer > > Centre for Plants and the Environment > University of Western Sydney > Hawkesbury Campus > Richmond NSW 2753 > > Dept of Biological Science > Macquarie University > North Ryde NSW 2109 > Australia > > Mobile: +61 (0)422 096908 > www.remkoduursma.com > > __ > R-help@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. > [[alternative HTML version deleted]] __ R-help@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.
Re: [R] How to make R automatic?
?difftime ?file.info file.info(filename)$mtime Sys.sleep(20) Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com On Jun 1, 2010, at 10:07 AM, zhangted001 wrote: Hello, I have a question about how R can run automatically. Here is the story: A file called "data.csv" will be generated every couple of minutes in a folder (overwrite itself). What I want R to do is: 1) scan the folder to find the file. 2) if the file is a newly generated file, process the file 3) output some file identifier on the screen, such as the time when the file was generated. 4) pause for 20 seconds (doesnt need to be precise) and repeat 1) Idealy, I would like R to do this everyday from 9:00am to 6:00pm. What I got so far is: I can use list.files() and file.info to get 1) and 2). For 4), I can write some trivial loop to kill some time, but I would like to be more accurate tham that, considering different PC running the loop. I have no idea about 3). Any suggestion/better solution? Thank you very much!! Best regards, Ted -- View this message in context: http://r.789695.n4.nabble.com/How-to-make-R-automatic-tp2238541p2238541.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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@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.
Re: [R] rounding up to nearest integer
?round On May 24, 2010, at 9:26 PM, Mohan L wrote: Dear All, I have a data frame "data" and the below is the str of "data" : $ Feb : int 1 1195 0 11 28 152 24 2 1 1470 ... $ Mar : int 0 1212 0 17 27 184 15 1 1 1311 ... $ Apr : int 2 1244 1 15 23 135 11 0 1 991 ... $ May : int 2 1158 2 10 23 111 16 1 1 1237 ... $ Jun : int 0 845 1 9 16 86 16 2 1 1129 ... $ Jul : int 0 832 0 7 16 68 9 1 0 994 ... $ Aug : int 0 1107 1 4 25 144 7 0 3 1260 ... $ Sep : int 2 1278 1 8 53 212 14 0 3 1375 ... $ Oct : int 3 1329 0 8 39 201 13 0 0 1340 ... $ Nov : int 1 1179 0 5 7 135 2 0 0 1153 ... $ Dec : int 0 1271 0 7 34 168 5 1 2 1792 ... $ Jan.10: int 0 1405 1 10 55 245 26 2 4 2806 ... $ Feb.10: int 0 1330 1 9 29 360 27 3 6 3492 ... $ Mar.10: int 0 1727 0 8 7 341 8 2 4 4578 ... $ Apr.10: int 0 1530 1 8 12 144 7 2 2 5453 ... I am doing some this like this : x <- (data[,-(1:2)] - data[,2]) * prop.table(data[,2]) + data[,2] x Feb Mar Apr May Jun Jul 1 1.000.5971.4031.4030.597 0.597 2 1292.610851 1293.5003225 1295.1746211 1290.6749436 1274.2982103 1273.6180264 Now the str of "x" is : str(x) 'data.frame':32 obs. of 15 variables: $ Feb : num 1 1293 0 5 18 ... $ Mar : num 1 1294 0 5 18 ... $ Apr : num 1 1295 0 5 18 ... $ May : num 1 1291 0 5 18 ... $ Jun : num 1 1274 0 5 18 ... $ Jul : num 1 1274 0 5 18 ... $ Aug : num 1 1288 0 5 18 ... $ Sep : num 1 1297 0 5 18 ... $ Oct : num 1 1300 0 5 18 ... $ Nov : num 1 1292 0 5 18 ... $ Dec : num 1 1297 0 5 18 ... $ Jan.10: num 1 1303.6 0 5 18 ... $ Feb.10: num 1 1300 0 5 18 ... $ Mar.10: num 1 1320 0 5 18 ... $ Apr.10: num 1 1310 0 5 18 ... I need to round up the data frame some thing like this : Feb Mar Apr May Jun Jul 1 1 11 1 1 1 2 1293 1294 1295 1291 1274 1274 there may be a way to round up the nearest integer. any help will be greatly appropriated. Thanks & Rg Mohan L [[alternative HTML version deleted]] __ R-help@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@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.
Re: [R] sort a data.frame
Try this. dd[order(gsub("chr","",dd$b)),] You need regular expressions if chr is not the only characterstring that is prepended to the numbers. look for ?strsplit Nikhil Kaza University of North Carolina nikhil.l...@gmail.com On May 20, 2010, at 8:28 AM, Yuan Jian wrote: Hello, I have a dataframe: dd <- data.frame(b = c("chr2", "chr1", "chr15", "chr13"), x = c("A", "D", "A", "C"), y = c(8, 3, 9, 9), z = c(1, 1, 1, 2)) dd b x y z 1 chr2 A 8 1 2 chr1 D 3 1 3 chr15 A 9 1 4 chr13 C 9 2 Now I want to sort them according column "b", but only its number is considered: b x y z 1 chr1 D 3 1 2 chr13 C 9 2 3 chr15 A 9 1 4 chr2 A 8 1 thanks jian [[alternative HTML version deleted]] __ R-help@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@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.
Re: [R] Adding a row at top of dataframe
My mistake. cars2 should be initalized to have all the extra rows. cars2 <- data.frame(matrix(rep(NA, prod(dim(cars)) + ncol(cars)), nrow(cars)+1)) cars2[2:nrow(cars2),] <- cars In this way, insertion at any row is possible. Nikhil On May 17, 2010, at 2:46 PM, Peter Ehlers wrote: data(cars) cars2 <- cars cars2[2:nrow(cars)+1,] <- cars2[1:nrow(cars),] cars2[1,] <- NA [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Adding a row at top of dataframe
Works with warnings for me. but your method is better. Nikhil On May 17, 2010, at 1:35 PM, Peter Ehlers wrote: data(cars) cars2 <- cars cars2[2:nrow(cars)+1,] <- cars2[1:nrow(cars),] cars2[1,] <- NA __ R-help@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.
Re: [R] Adding a row at top of dataframe
Does this work? data(cars) cars2 <- cars cars2[2:nrow(cars)+1,] <- cars2[1:nrow(cars),] cars2[1,] <- NA Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com On May 17, 2010, at 11:28 AM, ecvet...@uwaterloo.ca wrote: I have a large data frame 48:2185 with different numbers. I would like to add only one row at the very top of my data frame with 0's or NA's. I don't know which approach to use. Should i create 2 different data frames and merge them? Ive also tried the rbind command with no luck. I would appreciate some help to achieve what I'm trying to create. Thanks! __ R-help@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@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.
Re: [R] All possible paths between two nodes in a flowgraph using igraphs?
Finding all paths between two nodes in a general graph is very hard. If your graph is sparse you may be able to construct the list of paths provided of course you take care not to get stuck in a cycle. But for most practical purposes you may just need edge disjoint path or vertex disjoint paths. I am not sure about cycles. But I suppose you can just use the minimum spanning tree and iteratively add the remaining edges to get the cycles. Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com On May 4, 2010, at 7:34 AM, jcano wrote: Hi all Is there any systematic way to compute all possible paths, first- order loops and j-th order loops between two given nodes in a flowgraph (directed graph with cycles) - preferably using the igraph library in R? I have checked the igraph documentation but I can't figure out any direct and systematic way to do so. Any ideas? I use the following definitions from Butler, R. and A. Huzurbazar (1997). Stochastic Network Models for Survival Analysis. Journal of the American Statistical Association 92 (437), 246-257. - A path from node i to j is any possible sequence of nodes from i to j which does not pass through any intermediate node more than once. - A first-order loop is any closed path in the flowgraph that returns to the initial node of the loop without passing through any intermediate node more than once. - A jth-order loop consists of j nontouching first-order loops. For example, in the flowgraph below there are 18 paths between nodes 1 and a: - 1a; - 12a, 124a, 1243a, 1245a, 12436a, 124365a, 12456a, 124563a; - 13a, 134a, 136a, 1342a, 1345a, 13456a, 1365a, 13654a, 136542a. 6 first-order loops: - 12431, 13421, 1245631, 1365421, 45634, 43654; and no loops of order two or more. Thanks in advance jcano http://n4.nabble.com/file/n2125347/flowgraph_subsume.jpg -- View this message in context: http://r.789695.n4.nabble.com/All-possible-paths-between-two-nodes-in-a-flowgraph-using-igraphs-tp2125347p2125347.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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@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.
Re: [R] Text dependent on a variable in a R function
Thats right. serves me for not checking the code before posting. but paste should work in anycase with collapse or when x is a single parameter. Nikhil On Sun, May 2, 2010 at 10:24 AM, David Winsemius wrote: > > On May 2, 2010, at 10:10 AM, Nikhil Kaza wrote: > > say x is the variable. >> >> plot(..., title=paste(x, "whatever else"), ...) should work as well. >> >> same should work with file names as well. >> > > Perhaps in an alternate universe it might, ... but it doesn't in this one. > "title" is not the correct parameter for specifying titles, and if you > change the parameter name to the correct parameter name, "main", you are > now giving it a vector with text-coercion of the number of values of x in x > pasted to "whatever", since paste is vectorized. > > Not what you want: > > x <-c(-1, -0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75, 1) > plot(x, main=paste(x, "whatever")) > > -- > David > > > >> Nikhil >> >> >> >> On May 1, 2010, at 9:56 PM, R K wrote: >> >> >>> Hello, >>> >>> I was wondering if someone could tell me how I can make text dependent on >>> a variable in a R function I have created. >>> >>> The function will create plots, thus I would like each plot to have a >>> unique title based on the inputted variable as well as a unique file name >>> when saved using the savePlot function. >>> >>> Thanks! >>> Rachel >>> >>> _ >>> The New Busy is not the old busy. Search, chat and e-mail from your >>> inbox. >>> >>> N:WL:en-US:WM_HMP:042010_3 >>>[[alternative HTML version deleted]] >>> >>> __ >>> R-help@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@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. >> > > David Winsemius, MD > West Hartford, CT > > [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Text dependent on a variable in a R function
say x is the variable. plot(..., title=paste(x, "whatever else"), ...) should work as well. same should work with file names as well. Nikhil On May 1, 2010, at 9:56 PM, R K wrote: Hello, I was wondering if someone could tell me how I can make text dependent on a variable in a R function I have created. The function will create plots, thus I would like each plot to have a unique title based on the inputted variable as well as a unique file name when saved using the savePlot function. Thanks! Rachel _ The New Busy is not the old busy. Search, chat and e-mail from your inbox. N:WL:en-US:WM_HMP:042010_3 [[alternative HTML version deleted]] __ R-help@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@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.
Re: [R] Problem with optimization (constrOptim)
Ah..constrOptim is for linear inequality constraints. your ci is a matrix. it should be a vector. Nikhil On Apr 29, 2010, at 3:14 AM, Cz³owiek Kuba wrote: > Hi, > > You are right, my intention was to return a set of values and to > minimize them all in a multicriteria optimization problem. > > The interesting thing is that when I actually used scalar return of > this function, by minimizing sum of squares in this form: > > > fr <- function(z) { > t(z%*%matrix(c(2,5,6), 3,1)-matrix(c(5,4,2), 3,1))%*%(z%* > %matrix(c(2,5,6), 3,1)-matrix(c(5,4,2), 3,1)) > } > constrOptim((matrix(c(0,0,0,0,0,0,0,0,0),3,3)), fr) > or > nlm(fr, matrix(c(0,0,0,0,0,0,0,0,0),3,3)) > -- > the function also returned non-comformable error. > Kind regards > Jacob > > > > 2010/4/29 Nikhil Kaza > > fr does not return a scalar. > > > Nikhil > > > > On Apr 28, 2010, at 3:35 AM, Cz³owiek Kuba wrote: > > Hello, > > I have the following problem: > I have a set of n matrix equations in the form of : > [b1] = [A] * [b0] > [b2] = [A] * [b1] > etc. > vertical vectors [b0], [b1], ... are GIVEN. We try to estimate > matrix A. As > there are many equations (more than cells in matrix A) the system > has no > solutions. > A is transition matrix (stochastic matrix) or markov process, so the > sum of > each row = 1 and each entry is probability (aij in <0;1>). I tried to > estimate A by using constrOptim the following way, but apparently it > won't > work on matrices. > > fr <- function(x) { > x%*%matrix(c(2,5,6), 3,1)-matrix(c(5,4,2), 3,1) > x%*%matrix(c(6,2,3), 3,1)-matrix(c(1,1,1), 3,1) > x%*%matrix(c(6,1,2), 3,1)-matrix(c(3,4,1), 3,1) > } > constrOptim(matrix(c(0.5,0.4,0.1,0.2,0.3,0.5,0.5,0.2,0.3),3,3), fr, > NULL, > ui=matrix(c(1,0,0,0,1,0,0,0,1),3,3), ci=matrix(c(-.1 > ,-.1,-.1,-.1,-.1,-.1,-.1,-.1,-.1), > 3,3)) > > It produces the following error: > "Error in ui %*% theta : non-conformable arguments" > > Kind regards and thanks for help > Jacob > >[[alternative HTML version deleted]] > > __ > R-help@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. > > [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Problem with optimization (constrOptim)
fr does not return a scalar. Nikhil On Apr 28, 2010, at 3:35 AM, Człowiek Kuba wrote: Hello, I have the following problem: I have a set of n matrix equations in the form of : [b1] = [A] * [b0] [b2] = [A] * [b1] etc. vertical vectors [b0], [b1], ... are GIVEN. We try to estimate matrix A. As there are many equations (more than cells in matrix A) the system has no solutions. A is transition matrix (stochastic matrix) or markov process, so the sum of each row = 1 and each entry is probability (aij in <0;1>). I tried to estimate A by using constrOptim the following way, but apparently it won't work on matrices. fr <- function(x) { x%*%matrix(c(2,5,6), 3,1)-matrix(c(5,4,2), 3,1) x%*%matrix(c(6,2,3), 3,1)-matrix(c(1,1,1), 3,1) x%*%matrix(c(6,1,2), 3,1)-matrix(c(3,4,1), 3,1) } constrOptim(matrix(c(0.5,0.4,0.1,0.2,0.3,0.5,0.5,0.2,0.3),3,3), fr, NULL, ui=matrix(c(1,0,0,0,1,0,0,0,1),3,3), ci=matrix(c(-.1 ,-.1,-.1,-.1,-.1,-.1,-.1,-.1,-.1), 3,3)) It produces the following error: "Error in ui %*% theta : non-conformable arguments" Kind regards and thanks for help Jacob [[alternative HTML version deleted]] __ R-help@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@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.
Re: [R] Convert character vector into string
?cat On Apr 28, 2010, at 11:21 PM, Ian Seow wrote: Hi, how do I convert a character vector into a string? c("a","b","c") into "a b c" Thanks! [[alternative HTML version deleted]] __ R-help@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@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.
Re: [R] Finding the max correlation coefficient value using CCF function
try this ?which.max Nikhil Kaza Asst. Professor, City and Regional Planning University of North Carolina nikhil.l...@gmail.com On Apr 27, 2010, at 11:42 PM, vikrant wrote: Hi All, Suppose I have 2 time series a = 1:20 b = 5:15 and I am finding the cross correlation between these two time series using CCF function. c = ccf(a,b) print(c) Autocorrelations of series ‘X’, by lag -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 -0.358 -0.255 -0.110 0.070 0.278 0.507 0.750 1.000 0.750 0.507 0.278 0.070 -0.110 -0.255 -0.358 it will give me the correlation coefficient (r) with corrsponding lags. My Question is how to find the value of maximum correlation coefficient with the corrsponding lag. here in this case its r = 0.75 with lag = 0 how to get this valus in a vector or variable? -- View this message in context: http://r.789695.n4.nabble.com/Finding-the-max-correlation-coefficient-value-using-CCF-function-tp2068580p2068580.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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@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.
Re: [R] 2 simple question
If I understand it correctly par(mfrow=c(2,2)) plot(x,y) plot(y,z) . should work. On Apr 24, 2010, at 8:11 AM, Jim Lemon wrote: On 04/24/2010 02:52 AM, tamas barjak wrote: Hi All! I have 2 plain questions: 1.) I know that very primitive question, but that to grant it, that the drawing on the screen divided up onto which part draw for example: layout(matrix(1:4,ncol=2, byrow=T)) plot(x, y, ...)<--- 1. screen plot(y, z, ...)<--- 2. screen etc... 2.) How I can fix it and to insert the random numbers in order for him to generate them later for example: a<-runif(100) and to insert these here---> rnorm(100, 0, 1) Hi Tamas, I may not understand what you are asking, but take a look at the example for the "panes" function in the plotrix package. Jim __ R-help@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@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.
Re: [R] is.na<- doesn't seem to work with labelled variables?
a <- c(1, 8, 9, 10, 8) a[which(a==8)] <- NA Nikhil On Apr 6, 2010, at 7:31 AM, David Foreman wrote: Dear All, I seem entirely unable to solve what should be a very simple problem. I have imported a SPSS dataset into R using spss.get from Frank Harrell's Hmisc library. The variables are imported classed as 'labelled': missing values are coded as either the SPSS missing value code, 8 or 88. All are imported correctly; 8 and 88 being identified as true numbers in the 'summary' command, which treats them (correctly) as numeric. For some reason, is.na(x)<-c(8,88) doesn't seem to work. No error message is returned, but the 8 and 88 are not set as NA. The example given in the help file does work, so is.na<- is functioning in my copy of R (2.10.1). I've tried working outside the dataframe, using unclass, as.numeric, and class<- entirely without success. The variable structure(exemplified by one variable) is Class 'labelled' atomic [1:827] 2 2 2 2 2 2 8 NA 2 2 ... ..- attr(*, "label")= Named chr "In the last yr, you were unfairly treated because of your sex" .. ..- attr(*, "names")= chr "c25a" All suggestions are gratefully received! Best wishes David Foreman Consultant and Visiting Professor in Child and Adolescent Psychiatry [[alternative HTML version deleted]] __ R-help@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@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.
Re: [R] Optim() Help, Unusual Error
I cannot replicate the error. The following seem to work. Y <- runif(100) comp <- matrix(runif(6500),100,65) par <- rep(.5, 28) optim.results <- optim(par, fn=objective.function, method="Nelder- Mead",comp=comp, Y=Y, n=100) # Not sure why you are selecting the columns in the comp. That is probably the error Nikhil On Mar 23, 2010, at 3:38 PM, ApproxGaussian wrote: I apologize, the "c" is in the original coding; I merely misprinted (copy and paste). I have edited the orginal post to reflect this. -- View this message in context: http://n4.nabble.com/Optim-Help-Unusual-Error-tp1679363p1679582.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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@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.
Re: [R] Optim() Help, Unusual Error
missed a c(...) here, therefore no par defined. On Mar 23, 2010, at 12:58 PM, ApproxGaussian wrote: par<-(e.1,e.2,e.3,e.4,e.5,e.6,e.7,e.8,e.9,e.10,e.11,e.12,e.13,e.14,e. 15,e.16,e.17,e.18,e.19,e.20,e.21,e.22,e.23,e.24,e.25,e.26,e.27,e.28) __ R-help@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.
Re: [R] Delete all object except some particular ones
rm(setdiff(ls(), c("AA", "BB")) should work. On Mar 20, 2010, at 12:27 PM, bogaso.christofer wrote: Dear all, in my working project, I have created huge number of different kind of objects including "AA" and "BB". Now I want to delete all objects except that "AA" and "BB". Is there any procedure in R to do that ? Thanks, [[alternative HTML version deleted]] __ R-help@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@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.
Re: [R] Selecting single TAZ based on area proportion
try this t <- TazProperties.. v <- aggregate(t$Area, by=list(t$TAZ,t$Props), sum) names(v) <- c("TAZ", "Prop", "area") tapply(v$area, v$Prop, function(x) v$TAZ[which.max(x)]) note that you have to deal with the cases where there is a tie for the maximum. The above just returns the first maximum. Nikhil On Feb 8, 2010, at 1:14 PM, LCOG1 wrote: Good day all, I am having an issue coercing my data. Below i have some data on taxlots and an associated TAZ(transportation analsysi zone) that each property is within. The main issue is that some properties cross TAZ boundaries so i need to make a decision as to which TAZ the property belongs too, i would like to do this based on the area of the Property and ultimately assign the the property to the TAZ in which the majority or the most of the area is within. For instance in the data below: Property p754921 is in two TAZs, 38 and 37. Since the property is mostly in 37 i would like to assign this value to the list of properties(Props) so that MultiTazProperties_ goes from $p754921 [1] 38 37 37 $p75506 [1] 171 171 282 171 $p75508 [1] 46 46 169 169 169 to $p754921 [1] 37 $p75506 [1] 282 $p75508 [1] 46 PropsTAZ Area 1 p754921 38 109828.040 2 p754921 37 128134.710 3 p754921 37 46469.570 4 p75506 171 37160.210 5 p75506 171 40080.500 6 p75506 282 344679.660 7 p75506 171 16972.280 8 p75508 46 342309.558 9 p75508 46 260906.870 10 p75508 169 17014.659 11 p75508 169 7285.706 12 p75508 169 10936.316 #Data to use Props<-c("p754921","p754921" ,"p754921","p75506" ,"p75506" ,"p75506","p75506" ,"p75508","p75508","p75508","p75508","p75508") TAZ<-c(38,37,37,171,171,282,171,46,46,169,169,169) Area<-c(109828.04, 128134.71, 46469.57, 37160.21, 40080.50,344679.66,16972.28, 342309.558, 260906.870, 17014.659, 7285.706, 10936.316) TazProperties..<-data.frame(Props,TAZ,Area) MultiTazProperties_<-tapply(TAZ , Props, function(x) x) MultiTazArea_<-tapply(Area , Props, function(x) x) Hope my inquiry is clear. Thanks -- View this message in context: http://n4.nabble.com/Selecting-single-TAZ-based-on-area-proportion-tp1473288p1473288.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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@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.
Re: [R] Odp: Creating Dummy Variables in R
I don't think R will complain, if you use the approach below. However, IF, VVS1 and VVS2 are linearly dependent. Better use the factor approach and define which factor should be the contrast Nikhil On 16 Dec 2009, at 10:12AM, Petr PIKAL wrote: what commands did you use for regression I suppose lm(Price~Weight+IF+VVS1+VVS2, data=your.data) shall not complain if your.data is a data frame. Regards Petr __ R-help@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.
Re: [R] Changing Column names in (Output) csv file
I am not sure why you are reading and writing from disk so many times? It will degrade performance. Also avoid loops when you can. just use ONS$labels <- paste(ONS$Var1[i], ONS$Var2[i], ONS$Var3[i], ONS $Var4[i], ONS$Var5[i], ONS$Var6[i], ONS$Var7[i], ONS$Var8[i],ONS $Var9[i], ONS$Var10[i]) write.csv(c(ONS$labels, ONS$Freq), file="prob_table.csv") Alternately just use concatenate command in excel after you exported the joint probs. Nikhil On 15 Dec 2009, at 7:53AM, Amelia Livington wrote: Dear R helpers Following is a part of R code. data_lab <- expand.grid(c("R11", "R12", "R13"), c("R21", "R22", "R23"), c("R31", "R32", "R33"), c("R41", "R42", "R43"), c("R51", "R52", "R53"), c("R61", "R62", "R63"), c("R71", "R72", "R73"), c("R81", "R82", "R83"), c("R91", "R92", "R93"), c("R101", "R102", "R103")) range_prob <- list() range_prob[[1]] <- c(0.42,0.22,0.36) range_prob[[2]] <- c(0.14,0.56,0.30) range_prob[[3]] <- c(0.61,0.38,0.01) range_prob[[4]] <- c(0.34,0.37,0.29) range_prob[[5]] <- c(0.09,0.19,0.72) range_prob[[6]] <- c(0.42,0.21,0.37) range_prob[[7]] <- c(0.44,0.07,0.49) range_prob[[8]] <- c(0.54,0.06,0.40) range_prob[[9]] <- c(0.26,0.62,0.12) range_prob[[10]] <- c(0.65,0.19,0.16) pdf <- expand.grid(range_prob) data_lab$probs <- apply(pdf, 1, prod) joint_probs = xtabs(probs ~ Var1 + Var2+Var3+Var4+Var5+Var6+Var7+Var8+Var9+Var10, data = data_lab) write.csv(data.frame(joint_probs), 'joint_probs.csv', row.names = FALSE) ONS = read.csv('joint_probs.csv') Names = NULL for (i in 1:length(joint_probs)) { Names[i] = paste(ONS$Var1[i], ONS$Var2[i], ONS$Var3[i], ONS $Var4[i], ONS$Var5[i], ONS$Var6[i], ONS$Var7[i], ONS$Var8[i],ONS $Var9[i], ONS$Var10[i]) } write.csv(data.frame(labels = Names), 'Names.csv', row.names = FALSE) result = data.frame(read.csv('Names.csv')$labels, read.csv('joint_probs.csv')$Freq) write.csv(data.frame(result), 'prob_table.csv', row.names = FALSE) # The PROBLEM When I open the prob_table.csv file in Excel, instead of having column names as lables and Freq, I get the column heads as read.csv..Names.csv...labels read.csv..joint_probs.csv...Freq R11 R21 R31 R41 R51 R61 R71 R81 R91 R1011.85E - 5 and so on. Ideally I will like to have the column names as LabelProbability Please guide Amelia [[alternative HTML version deleted]] __ R-help@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@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.
Re: [R] Help with missing values in the dataset
?merge use all=L On 10 Dec 2009, at 6:06AM, Venkatesh.P wrote: Dear all, I am facing problem with inserting the scheduled day of Observation in the dataset. In the dataset I have only relative time (table 1) and not scheduled day of observation (day 1, 4, 8, 15, 22, 29, 36, 43). I would appreciate if any one could suggest me how to proceed. Eg: Table 1 The real dataset looks like this with Time, DV ... etc RTime DV 1101 495 886 15 96 25 80 29 69 I need to make the dataset (insert SDAY column and 2 rows for day 36 and 43 with dots in DV column) in following format SDAYRTime DV 1 1101 4 495 8 886 15 15 96 21 25 80 29 29 69 36 36 . Missing Observation 43 43 .Missing Observation Thank you in advance, Venkatesh Best Regards Venkatesh P The INTERNET now has a personality. YOURS! See your Yahoo! Homepage. [[alternative HTML version deleted]] __ R-help@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@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.
Re: [R] conditionally merging adjacent rows in a data frame
This is great!! Sqldf is exactly the kind of thing I was looking for, other stuff. I suppose you can speed up both functions 1 and 5 using aggregate and tapply only once, as was suggested earlier. But it comes at the expense of readability. Nikhil On 9 Dec 2009, at 7:59AM, Titus von der Malsburg wrote: On Wed, Dec 9, 2009 at 12:11 AM, Gabor Grothendieck wrote: Here are a couple of solutions. The first uses by and the second sqldf: Brilliant! Now I have a whole collection of solutions. I did a simple performance comparison with a data frame that has 7929 lines. The results were as following (loading appropriate packages is not included in the measurements): times <- c(0.248, 0.551, 41.080, 0.16, 0.190) names(times) <- c("aggregate","summaryBy","by +transform","sqldf","tapply") barplot(times, log="y", ylab="log(s)") So sqldf clearly wins followed by tapply and aggregate. summaryBy is slower than necessary because it computes for x and dur both, mean /and/ sum. by+transform presumably suffers from the contruction of many intermediate data frames. Are there any canonical places where R-recipes are collected? If yes I would write-up a summary. These were the competitors: # Gary's and Nikhil's aggregate solution: aggregate.fixations1 <- function(d) { idx <- c(TRUE,diff(d$roi)!=0) d2 <- d[idx,] idx <- cumsum(idx) d2$dur <- aggregate(d$dur, list(idx), sum)[2] d2$x <- aggregate(d$x, list(idx), mean)[2] d2 } # Marek's symmaryBy: library(doBy) aggregate.fixations2 <- function(d) { idx <- c(TRUE,diff(d$roi)!=0) d2 <- d[idx,] d$idx <- cumsum(idx) d2$r <- summaryBy(dur+x~idx, data=d, FUN=c(sum, mean))[c("dur.sum", "x.mean")] d2 } # Gabor's by+transform solution: aggregate.fixations3 <- function(d) { idx <- cumsum(c(TRUE,diff(d$roi)!=0)) d2 <- do.call(rbind, by(d, idx, function(x) transform(x, dur = sum(dur), x = mean(x))[1,,drop = FALSE ])) d2 } # Gabor's sqldf solution: library(sqldf) aggregate.fixations4 <- function(d) { idx <- c(TRUE,diff(d$roi)!=0) d2 <- d[idx,] d$idx <- cumsum(idx) d2$r <- sqldf("select sum(dur), avg(x) x from d group by idx") d2 } # Titus' solution using plain old tapply: aggregate.fixations5 <- function(d) { idx <- c(TRUE,diff(d$roi)!=0) d2 <- d[idx,] idx <- cumsum(idx) d2$dur <- tapply(d$dur, idx, sum) d2$x <- tapply(d$x, idx, mean) d2 } __ R-help@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@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.
Re: [R] conditionally merging adjacent rows in a data frame
How about creating an index using multiple columns. a <- with(d, aggregate(dur, list(rt=rt,tid=tid,mood=mood,roi=roi), sum)) b <- with(d, aggregate(x, list(rt=rt,tid=tid,mood=mood,roi=roi), mean)) c <- merge(a, b, by=c("rt","tid","mood", "roi")) I suppose one could save some time by not running aggregate twice on the same dataset, but I am not sure how. Nikhil On 8 Dec 2009, at 7:50AM, Titus von der Malsburg wrote: Hi, I have a data frame and want to merge adjacent rows if some condition is met. There's an obvious solution using a loop but it is prohibitively slow because my data frame is large. Is there an efficient canonical solution for that? head(d) rt dur tid mood roi x 55 5523 200 4 subj 9 5 56 5523 52 4 subj 7 31 57 5523 209 4 subj 4 9 58 5523 188 4 subj 4 7 70 4016 264 5 indic 9 51 71 4016 195 5 indic 4 14 The desired result would have consecutive rows with the same roi value merged. dur values should be added and x values averaged, other values don't differ in these rows and should stay the same. head(result) rt dur tid mood roi x 55 5523 200 4 subj 9 5 56 5523 52 4 subj 7 31 57 5523 397 4 subj 4 8 70 4016 264 5 indic 9 51 71 4016 195 5 indic 4 14 There's also a solution using reshape. It uses an index for blocks d$index <- cumsum(c(TRUE,diff(d$roi)!=0)) melts and then casts for every column using an appropriate fun.aggregate. However, this is a bit cumbersome and also I'm not sure how to make sure that I get the original order of rows. Thanks for any suggestion. Titus __ R-help@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@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.
Re: [R] Help with Nested loop - very slow, can I use an apply?
From an old post by Gabor http://tolstoy.newcastle.edu.au/R/help/04/01/0147.html apply (outer (landmark_c,t(store_c),"-"),c(1,4),function(x)sqrt(sum(diag(x*x On 7 Dec 2009, at 10:58PM, dolar wrote: Hi there I have two tables, with longitudinal and latitudinal coordinates. what I want is a cross table between each coordinate, to find the distance between each building and different landmarks I currently have this nested loop which is fine for when i have 10 stores of interest, against 200 other landmarks and uses pythagarus to find the distance for(i in 1:nrow(store_c)){ landmark_store_cross_tab$foo=landmark_c$landmark_name for (j in 1:nrow(landmark_c)){ landmark_store_cross_tab[j,i+1]=sqrt((landmark_c$x[j]-store_c$x[i])^2 +(landmark_c$y[j]-store_c$y[i])^2)/1000 } names(landmark_store_cross_tab)[i+1]=store_c$store_name[i] } can someone suggest how I can make this faster? I am hoping to use one of the apply functions Thanks -- View this message in context: http://n4.nabble.com/Help-with-Nested-loop-very-slow-can-I-use-an-apply-tp954979p954979.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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@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.
Re: [R] How to get the item in list that is a number?
?unlist if I understand you correctly. On 24 Nov 2009, at 5:50PM, Peng Yu wrote: I have the following list. The second item in the list is a number. I'm wondering how to write R code to return this information for any list? $`1` integer(0) $`2` [1] 123 $`3` integer(0) $`4` integer(0) $`5` integer(0) __ R-help@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@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.
Re: [R] Do you keep an archive of "useful" R code? and if so - how?
I 've used tiddlywiki a personal notebook for other things but not for R. It may be useful to write a css that separates out code from description. http://www.tiddlywiki.com/ On 22 Nov 2009, at 11:53AM, Tal Galili wrote: Hello all, When using R for some time, one comes across more and more useful functions. But naturally we can't remember all of them, so I imagine some of you save these snippets of code. My question to you is how do you manage that code? Do you use special software, or archiving system? Any advice is welcomed. Tal Contact Details:--- Contact me: tal.gal...@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com/ (English) -- [[alternative HTML version deleted]] __ R-help@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@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.
Re: [R] optim(.. ,"SANN",..)
I think the issue is in the function fr, ?apply apply returns a vector or array or list of values So if the inner apply returns a list (this happens when different number of elements in y are positive in different rows) then outer apply cannot coerce it into the correct format to apply the product To see this try set.seed(1) v<- runif(12, -10,10) fr(v) Nikhil On 19 Nov 2009, at 6:05PM, lloyd barcza wrote: > fr<-function(x){y<-x*D > C<-apply(t(apply(y,1,function(c){c[c>0]})),1,prod) > R2<-R*C > w<-R2[R2 g<-sum(w) > return(g)} [[alternative HTML version deleted]] __ R-help@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.
Re: [R] How to transform the Matrix into the way I want it ???
This is not an answer to your question, but I have used SparseM package to represent large travel time matrices efficiently. ?as.matrix.ssr if the traveltime matrix is symmetric. On 9 Nov 2009, at 5:24PM, Hongwei Dong wrote: Hi, R users, I'm trying to transform a matrix A into B (see below). Anyone knows how to do it in R? Thanks. Matrix A (zone to zone travel time) zone z1 z2 z3 z1 0 2.9 4.3 z2 2.9 0 2.5 z3 4.3 2.5 0 B: from to time z1 z1 0 z1 z2 2.9 z1 z3 4.3 z2 z1 2.9 z2 z2 0 z2 z3 2.5 z3 z1 4.3 z3 z2 2.5 z3 z3 0 The real matrix I have is much larger, with more than 2000 zones. But I think it should be the same thing if I can transform A into B. Thanks. Garry [[alternative HTML version deleted]] __ R-help@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@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.
Re: [R] Find the first values in vector
How about vec[1:min(which(vec==FALSE))-1] This will return a character(0) vector if vec[1] is FALSE Nikhil On 9 Nov 2009, at 2:38PM, David Winsemius wrote: vec= TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE TRUE TRUE FALSE __ R-help@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.
Re: [R] Frequency
try sort (table(MAT), decreasing=T) if MAT is your matrix I think this is what you want. though if you want to sort by the first occurrence then it is a different story. Nikhil On 2 Nov 2009, at 1:35PM, Val wrote: V1 v2 v3 v4 569 10 347 10 46 10 18 __ R-help@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.
Re: [R] Re ading and Creating Shape Files
Have you tried readShapePoints writePointsShape both in maptools. Also, I don't think you really need a proj4string specified unless you are actually doing some spatial operations such as buffers and distances. I take it that you have already considered and discarded the possibility of using using a unique ID for the tax records and after the analysis in R just joining the resulting table in a GIS to your original shapefile. Nikhil On 28 Oct 2009, at 1:33PM, PDXRugger wrote: Hello R Community, I have imported a dataset which contain X Y coordinates and would like to recreate a shape file after some data analysis. What i have done is to import some taxlot data and join them based on some criteria. I want to check to see how well the joining went by reviewing the results in GIS. A couple things. I cant seem to import a shape file correctly using the maptools package and the readShapeSpatial. I have tried Building=file("data/input/BuildingShape/Building.shp") Bldg<-readShapeSpatial(fn=data/input/BuildingShape/ Building,proj4string=NAD83) #-- Bldg<-readShapeSpatial(data/input/BuildingShape/ Building,proj4string=NAD83) #--- Building=file("data/input/BuildingShape/Building.shp") Bldg<-readShapeSpatial(Building,proj4string=NAD83) I know i am mis interpreting the documentation but it doesnt seem like it is very complicated so i am of course confused. Also, i am wondering if i can create a shape file by simply using XY coordinates from a data frame. So for: Ycoord=c( 865296.4, 865151.5, 865457.0 ,865363.4 ,865311.0, 865260.9 ,865210.7 ,865173.3, 865123.6 ,865038.2 ,864841.1 ,864745.4 ,864429.1 ,864795.6 ,864334.9 ,864882.0) Xcoord=c( 4227640 ,4227816 ,4228929 ,4228508 ,4229569 ,4229498 , 4226747, 4226781, 4229597, 4229204, 4228910, 4228959 ,4229465 ,4229794 ,4229596 ,4229082) Lot<-c(1900 , 2000, 2100 , 100 ,200 , 300, 400 , 500 , 600 , 701 , 900 , 1000 , 1100, 300 ,100, 200) XYcoord<-spCbind(Ycoord,Xcoord) #doesnt work so XYcoord=c(Ycoord,Xcoord) TaxLots<-cbind(Ycoord,Xcoord,Lot) writeSpatialShape(XYcoord, TaxLots.., file=data/input/test/Taxlots,strictFilename=FALSE) So help reading in shape files and then creating them using XY coordinates if possible Any help would be appreciated. Thank you. -- View this message in context: http://www.nabble.com/Reading-and-Creating-Shape-Files-tp26098828p26098828.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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@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] Recode issue
I am having trouble with the recode function that is provided in the CAR package. I trying to create a new factors based on existing factors. E.g. >x <- as.factor(1:20) >y <- recode(x, " 1:5='A'; 6:10='B'; 11:15='C'; 16:20='D' ") >y [1] A A A A A 6 7 8 9 A A A A A A A A A A A Levels: 6 7 8 9 A Could someone point to me, my error? Thanks __ R-help@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.