Re: [R] Rounding variables in a data frame
Bill, Joshua, Phil Thanks for the suggestions. Very much appreciated. Kind regards Pete -- View this message in context: http://r.789695.n4.nabble.com/Rounding-variables-in-a-data-frame-tp3218729p3219060.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] Rounding variables in a data frame
Hi All I am trying to use the round function on some columns of a dataframe while leaving others unchanged. I wish to specify those columns to leave unchanged. My attempt is below - here, I would like the column d3 to be left but columns d1, d2 and d4 to be rounded to 0 decimal places. I would welcome any suggestions for a nicer way of doing this. d1= rnorm(10,10) d2= rnorm(10,6) d3= rnorm(10,2) d4= rnorm(10,-4) d = data.frame(d1,d2,d3,d4) x= NULL for (i in 1:ncol(d)){ if (colnames(d)[i] ==d3){x[i] = d[i] } else { x[i] = round(d[i])} out = do.call(cbind,x) } colnames(out) = colnames(d) Thanks and regards Pete -- View this message in context: http://r.789695.n4.nabble.com/Rounding-variables-in-a-data-frame-tp3218729p3218729.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] zoo column names
Hi All I am trying to find a way to prevent column names from changing on zoo objects when I merge them. I have column names that contain a - that after merging have become . # Example zz1 = zoo(as.matrix(1:5)) zz2 = zoo(as.matrix(10:12)) names(zz1)=c(aa-1) names(zz2)=c(bb-1) zz3=merge(zz1,zz2) print(zz3) Obviously, I could do something like names(zz3) = c(names(zz1),names(zz2)) but I have a large number of zoo objects that I am merging. Creating the simple line above is not that easy. Any thoughts would be gratefully received. Kind regards Pete -- View this message in context: http://r.789695.n4.nabble.com/zoo-column-names-tp3205568p3205568.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.
Re: [R] zoo column names
Achim Thanks. Much appreciated. Best regards Pete -- View this message in context: http://r.789695.n4.nabble.com/zoo-column-names-tp3205568p3205584.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] Merging zoo objects
Hi I have n zoo objects M1, M2, M3, ... , Mn that I want to merge where n is a number calculated at run-time. I am struggling to find the correct syntax to do this Assuming n is calculated as 10 (for example), I have tried n = 10 # First Effort alldata= merge(paste(M,rep(1:n), sep=),all=TRUE) # Second Effort alldata =merge(noquote(toString(paste(M,rep(1:nrow(counts1)),sep=))),all=TRUE) Any insights would be gratefully received. Kind regards Pete -- View this message in context: http://r.789695.n4.nabble.com/Merging-zoo-objects-tp3184696p3184696.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.
Re: [R] Merging zoo objects
Thanks Gabor. Much appreciated (as always). -- View this message in context: http://r.789695.n4.nabble.com/Merging-zoo-objects-tp3184696p3186113.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.
Re: [R] function()
If, as a new R user, you find the help page difficult to follow then Google offers plenty of support to understand the apply function e.g. http://www.ats.ucla.edu/stat/r/library/advanced_function_r.htm http://www.slideshare.net/dataminingtools/r-apply-functions http://nsaunders.wordpress.com/2010/08/20/a-brief-introduction-to-apply-in-r/ HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/function-tp3168116p3168352.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.
Re: [R] Good documentation about Sweave
Colin Some links I used to get me going ... http://www.ics.uci.edu/~vqnguyen/talks/SweaveSeminaR.pdf http://www.r-bloggers.com/getting-started-with-sweave-r-latex-eclipse-statet-texlipse/ http://www.statistik.lmu.de/~leisch/Sweave/ http://stat.epfl.ch/webdav/site/stat/shared/Regression/EPFL-Sweave-powerdot.pdf http://www.biostat.jhsph.edu/~rpeng/ENAR2009/lecture-slides.pdf HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Good-documentation-about-Sweave-tp2714326p2714560.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.
Re: [R] legend outside plot area
Emily Maybe this will help # Data y1=rnorm(10) y2=runif(10) x=1:10 # Set XPD and Outer Margin par(xpd=NA,oma=c(3,0,0,0)) # Plot and Legend plot(x,y1,type=n,ylab=) lines(x,y1, col=red) lines(x,y2, col=blue) legend(par(usr)[1],par(usr)[3],c(Y1,Y2),col=c(red,blue),lty=1,xjust=0, yjust=2.0) Kind regards Pete -- View this message in context: http://r.789695.n4.nabble.com/legend-outside-plot-area-tp2325864p2325938.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] Download xls file from internet
I am trying to download an Excel file from the the internet with the following R command download.file(http://ir.eia.gov/wpsr/psw09.xls,C:\\Temp\\psw09.xls;) The file is downloaded but when I try and open it I get a message from Excel telling me that the file is corrupt and it cannot be opened. If I manually download the file it opens without problems. Any insights would be gratefully received. Kind regards Pete -- View this message in context: http://r.789695.n4.nabble.com/Download-xls-file-from-internet-tp2315629p2315629.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.
Re: [R] adding a row of names to data.frame
Jim Is this what you need? #create data Lines - Drug1 Drug2 Drug3 Drug4 153 133 145 111 189 177 200 170 221 241 187 243 215 228 201 178 302 283 292 248 223 255 220 202 201 238 233 163 173 164 172 139 121 128 119 120 100 200 300 400 # read in data d - read.table(textConnection(Lines), header = TRUE) #add row.names row.names(d)=paste(trial_number,sprintf(%02d,as.numeric(row.names(d))),sep=) # view output print(d) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/adding-a-row-of-names-to-data-frame-tp2278278p2278345.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.
Re: [R] Factor to variable
Hi How about ... # Data lines = Col1 Col2 Col3 Col4 Text1 Text2 X0.1 5 Text1 Text2 X0.2 10 Text1 Text2 X0.3 15 Text1 Text3 X0.1 5 Text1 Text3 X0.2 10 Text1 Text3 X0.3 15 # Create Dataframe DF - read.table(textConnection(lines), header = TRUE, check.names = FALSE) # Create Numeric Variable DF$Col5 = as.numeric(sub(X,,DF$Col3)) HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Factor-to-variable-tp2244337p2244384.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.
Re: [R] Fwd: R apply() help -urgent
Venkatesh Is this what you are looking for? # Example data df=data.frame(A=c(1,0,0,0,1),B=c(1,1,0,0,0),C=c(1,0,0,0,0),Val=c(1,0,1,0,1)) # Variation of code of David Winsemius tbl = lapply(df[, 1:3] , function(x) table(x, df$Val)) fet = lapply(tbl, function(x) fisher.test(x)) # Identify internal objects of fet names(fet$A) # get p values p.val = do.call(rbind,fet)[,1] p.val.df=data.frame(pval=matrix(unlist(p.val))) # get conf int ci = do.call(rbind,fet)[,2] ci.df=data.frame(matrix(unlist(ci),byrow=TRUE,ncol=2)) # add back id of letter id = data.frame(idletter=colnames(df[,-4])) # results res = data.frame(id,pvalue=p.val.df,confint=ci.df) There is undoubtly a more elegant way of handling getting the p-values and conf int than my attempt. HTH Pete -- View this message in context: http://r.789695.n4.nabble.com/Fwd-R-apply-help-urgent-tp2164281p2164867.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.
Re: [R] How to set proxy settings for R
Hi Alessandra Did you restart R? You should be able to download packages using the Packages Install Package(s) ... from the menu bar in the GUI. Once the package has been downloaded select Packages Load Package ... you should see what you just downloaded in a list of available packages Pete From: danda [via R] [mailto:ml-node+2017172-614719349-63...@n4.nabble.com] Sent: Tuesday, April 20, 2010 5:02 AM To: Brecknock, Peter Subject: Re: How to set proxy settings for R Dear Pete, Thanks, it works now! I did as you suggested: --internet flag to the target line (right click, properties) e.g. C:\Program Files\R\R-2.8.1\bin\Rgui.exe --internet2 Strangely enough, I can now easly download packages but I still get these messages: chooseCRANmirror() Warning message: In open.connection(con, r) : cannot open: HTTP status was '407 Proxy Authentication Required' utils:::menuInstallPkgs() Warning: unable to access index for repository http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/2.10 ..but eventually it does install what I want...any clue? Alessandra View message @ http://n4.nabble.com/How-to-set-proxy-settings-for-R-tp2016158p2017172.h tml To unsubscribe from Re: How to set proxy settings for R, click here (link removed) ub2NrQGJwLmNvbXwyMDE2NTExfC0xNDg0OTk1MTQx . -- View this message in context: http://n4.nabble.com/How-to-set-proxy-settings-for-R-tp2016158p2017327.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.
Re: [R] merge
Maybe this what you are looking for lines1 - CODPROD N1 N3 N4 23 3 55 4 24 5 67 36 25 3 73 24 df1 - read.table(textConnection(lines1),header=TRUE) lines2 -CODPROD N1 N2 30 34 45 45 0 78 65 0 56 df2 - read.table(textConnection(lines2),header=TRUE) merge(df1, df2, by = intersect(names(df1),names(df2)), all=TRUE) HTH Pete -- View this message in context: http://n4.nabble.com/merge-tp2015796p2015966.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.
Re: [R] How to set proxy settings for R
Also, the FAQ suggests using the alternative internet2.dll by starting R with the flag --internet2 If you start R from a desktop icon, you can add the --internet flag to the target line (right click, properties) e.g. C:\Program Files\R\R-2.8.1\bin\Rgui.exe --internet2 see http://cran.r-project.org/bin/windows/rw-FAQ.html#The-Internet-download-functions-fail_002e HTH Pete -- View this message in context: http://n4.nabble.com/How-to-set-proxy-settings-for-R-tp2016158p2016511.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] Summarization
Hi All I have a vector x containing 2 levels x = c(1,1,1,0,0,0,0,1,1,0,0,0,1,1,0) I would like to derive the following summarization Level Count 1 3 0 4 1 2 0 3 1 2 0 1 I have generated an inelegant solution using lags and loops but feel sure that there must be a better approach. If anyone has any thoughts I would be very grateful if you would share them. Thanks and best regards Pete -- View this message in context: http://n4.nabble.com/Summarization-tp1840718p1840718.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.
Re: [R] Summarization
Dennis Very nice! Thanks Pete -- View this message in context: http://n4.nabble.com/Summarization-tp1840718p1840730.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] Rollapply
Hi I would like to understand how to extend the function (FUN) I am using in rollapply below. ## With the following simplified data, test1 yields parameters for a rolling regression data = data.frame(Xvar=c(70.67,70.54,69.87,69.51,70.69,72.66,72.65,73.36), Yvar =c(78.01,77.07,77.35,76.72,77.49,78.70,77.78,79.58)) data.z = zoo(d) test1 = rollapply(data.z, width=3, FUN = function(z) coef(lm(z[,1]~z[,2], data=as.data.frame(z))), by.column = FALSE, align = right) print(test1) ## Rewriting this to call myfn1 gives test2 (and is consistent with test1 above) myfn1 = function(mydata){ dd = as.data.frame(mydata) l = lm(dd[,1]~dd[,2], data=dd) c = coef(l) } test2 = rollapply(data.z, width=3, FUN= myfn1, by.column = FALSE, align = right) print(test2) ## I would like to be able to use the predict function to obtain a prediction (and its std error) from the rolling regression I have just calculated. My effort below issues a warning that 'newdata' had 1 row but variable(s) found have 3 rows. (if I run this outside of rollapply I don't get this warning) Also, I don't see the predicted value or its se with print(fm2[[1]]). Again, if I run this outside of rollapply I am able to extract the predicted value. Xpred=c(70.67) myfn2 = function(mydata){ dd = as.data.frame(mydata) l = lm(dd[,1]~dd[,2], data=dd) c = coef(l) p = predict(l, data.frame(Xvar=Xpred),se=T) ret=c(l,c,p) } fm2 = rollapply(data.z, width=3, FUN= myfn2, by.column = FALSE, align = right) print(fm2[[1]]) Any insights would be gratefully received. Best regards Pete -- View this message in context: http://n4.nabble.com/Rollapply-tp1013345p1013345.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.
Re: [R] merging issue.........
Try the merge function ?merge in1 = id trait1 110.2 211.1 39.7 610.2 78.9 10 9.7 11 10.2 in2 = id trait2 1 9.8 2 10.8 4 7.8 5 9.8 6 10.1 1210.2 1310.1 data1 = read.table(textConnection(in1), header=T) data2 = read.table(textConnection(in2), header=T) mymerge = merge(data1,data2,all.x=TRUE) print(mymerge) karena wrote: hi, I have a question about merging two files. For example, I have two files, the first file is like the following: id trait1 110.2 211.1 39.7 610.2 78.9 10 9.7 11 10.2 The second file is like the following: idtrait2 1 9.8 2 10.8 4 7.8 5 9.8 6 10.1 1210.2 1310.1 now I want to merge the two files by the variable id, I only want to keep the ids which show up in the first file. Even the id does not show up in the second file, it doesn't matter, I can keep the missing values. So my question is: how can I merge the two files and keep only the rows whose id show up in the first file? I know how to do it is SAS, just use the following code: merge data1(in=in1) data2(in=in2); by id; if in1; but I really have no idea about how to do it in R. thank you in advance, karean -- View this message in context: http://n4.nabble.com/merging-issue-tp1013356p1013375.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.
Re: [R] Operating on each row of data frame
Look at the apply function ?apply x = data.frame(x1=c(1,2,3,4,5),x2=c(2,4,6,8,10),x3=c(1,3,5,7,9)) x$x5=apply(x,1,mean) x$x6=apply(x,1,sd) print(x) Abhishek Pratap wrote: Hi All I have a data frame in which there are 4 columns . Column 1 : name Column 2-4 : values I would like to calculate mean/Standard error of values in column 2-4 and store them in column 5,6 respectively. I have done the following but doesn't seem to work mean_N_SE -function(x) { name - x[1] vals - c(x[2:4]) temp_mean - mean(vals) SE - sqrt(var(x)/length(x)) } apply(d,1,mean_N_SE) where d = data frame. Can someone help me with this. Thanks! -Abhi [[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. -- View this message in context: http://n4.nabble.com/Operating-on-each-row-of-data-frame-tp1013365p1013397.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.