[R] apply with multiple references and database interactivity
Hi R Colleagues, I have a small R script that relies on two for-loops to pull data from a database, make some edits to the data returned from the query, then inserts the updated data back into the database. The script works just fine, no problems, except that I am striving to get away from loops, and to focus on the apply family of tools. In this case, though, I did not know quite where to start with apply. I wonder if someone more adept with apply would not mind taking a look at this, and suggesting some tips as to how this could have been accomplished with apply instead of nested loops. More details on what the script is accomplishing are included below. Thanks in advance for your help and consideration. Steve Here, I have a df that includes a list of keywords that need to be edited, and the corresponding edit. The script goes through a database of people, identifies whether any of the keywords associated with each person are in the list of keywords to edit, and, if so, pulls in the list of keywords and the person details, swaps the new keyword for the old keyword, then inserts the updated keywords back into the database for that person (many keywords are associated with each person, and they are in an array, hence the somewhat complicated procedure). The if-statement provides a list of keywords in the df that were not found in the database, and 'm' is just a counter to help me know how many keywords the script changed. for(i in 1:nrow(keywords)) { pull - dbGetQuery(conn = con, statement = paste0(SELECT person_id, expertise FROM people WHERE expertise RLIKE '; , keywords[i, 2], ;')) pull$expertise - gsub(keywords[i, 2], keywords[i, 3], pull$expertise) if (nrow(pull)==0) { sink('~/Desktop/r1', append = TRUE) print(keywords[i, ]$keyword) sink() } else { for (j in 1:nrow(pull)) { dbSendQuery(conn = con, statement = paste0(UPDATE people SET expertise = ', pull[j, ]$expertise, ' WHERE person_id = , pull[j, ]$person_id)) } m=m+1 } } -- View this message in context: http://r.789695.n4.nabble.com/apply-with-multiple-references-and-database-interactivity-tp4711148.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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 there orphan code in seq.default?
On 08/15/2015 02:01 PM, David Winsemius wrote: I was looking at the code in seq.default and saw code that I think would throw an error if it were ever executed, although it will not because there is first a test to see if one of its arguments is missing. Near the end of the function body is this code: else if (missing(by)) { if (missing(to)) to - from + length.out - 1L if (missing(from)) from - to - length.out + 1L if (length.out 2L) if (from == to) rep.int(from, length.out) else as.vector(c(from, from + seq_len(length.out - 2L) * by, to)) Notice that the last call to `else` would be returning a value calculated with 'by' which was already established as missing. missing arguments can have default values f = function(by=sea) if (missing(by)) by f() [1] sea which is the case for seq.default args(seq.default) function (from = 1, to = 1, by = ((to - from)/(length.out - 1)), length.out = NULL, along.with = NULL, ...) Martin Morgan -- Computational Biology / Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: Arnold Building M1 B861 Phone: (206) 667-2793 __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Is there orphan code in seq.default?
I was looking at the code in seq.default and saw code that I think would throw an error if it were ever executed, although it will not because there is first a test to see if one of its arguments is missing. Near the end of the function body is this code: else if (missing(by)) { if (missing(to)) to - from + length.out - 1L if (missing(from)) from - to - length.out + 1L if (length.out 2L) if (from == to) rep.int(from, length.out) else as.vector(c(from, from + seq_len(length.out - 2L) * by, to)) Notice that the last call to `else` would be returning a value calculated with 'by' which was already established as missing. -- David Winsemius Alameda, CA, USA __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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
Hi Andre, You can do it manually like this: testmat-matrix(rnorm(90),ncol=3) boxplot(testmat,ylim=c(-3,4)) library(plotrix) draw.arc(1.5,2,0.5,0,pi,col=1) draw.arc(2.5,2,0.5,0,pi,col=1) boxed.labels(c(1.5,2.5),c(3.3,3.3),c(**,***),border=NA) Obviously you would have to nudge the arcs and labels around to match your plot. I don't know of a function that does this automatically. Also if your aspect ratio is far from 1, the arcs won't be circular. It is possible to write a function that will use the return values from boxplot to display rectangular brackets above the boxplots and then put the asterisks or whatever else you want on the top lines. Jim On Sat, Aug 15, 2015 at 5:46 AM, André Luis Neves andrl...@ualberta.ca wrote: Dear everyone, Would like to know how to add asterisks and arcs that indicate a subgroup comparison above box plots to denote statistical diference. Thank you. Andre [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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 -- To UNSUBSCRIBE and more, see 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] Error - cannot open file 'specdata/001.csv': No such file or directory; Windows 8, R Version 3.2.1
I am having a problem in opening the excel files in specdata folder. I have completed coding the R program for the assignment but when I run the following commands in the R console, *source(pollutantmean.R)* * pollutantmean(specdata, nitrate, 23)* I get an error message stating *Error in file(file, rt) : cannot open the connection* *In addition: Warning message:* *In file(file, rt) :* * cannot open file 'specdata/023.csv': No such file or directory* I tried everything and reset my Working Directory to *C:/Users/acer/My Documents/specdata/rprog-data-specdata/specdata* After the last specdata folder are all the excel sheets numbered 001 to 332. I searched the internet and all other options available, and got a solution to open it using the following command: *df - read.csv(specdata/001.csv)* This generated the following error message *Error in file(file, rt) : cannot open the connection* *In addition: Warning message:* *In file(file, rt) :* * cannot open file 'specdata/001.csv': No such file or directory* I have tried various other commands also such as *path - c(paste(./,directory, /,formatC(id[i], width=3, flag=0),.csv,sep=))* However, all the commands show some error. What shall I do? I am using the 3.2.1 version of R on a Windows 8 laptop. Regards, Nikita Dinger [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Cumulative vs. non-cumulative IRFs in R
I am using irf function from vars package. I am trying to derive cumulative IRFs. The following code describes the case of deriving cumulative IRFs: irf(vecm.l, impulse = c(g,p,h,l,s), response = g, cumulative = TRUE,n.ahead = 20, ortho=TRUE) I got the output and plotted it, it looked like cumulative values of estimated MA coefficients. But then I ran the code with cumulative switched to FALSE as below: irf(vecm.l, impulse = c(g,p,h,l,s), response = g, cumulative = FALSE,n.ahead = 20, ortho=TRUE) Output from these two codes is identical. Including cumulative = TRUEin the irf function does not produce the cumulative responses?? Please note that I converted a vecm model to var using vec2var, hence the input model is called vecm.l here. Though, I doubt it plays any significance in deriving cumulative IRFs. I would appreciate your comment, am I specifying the function incorrectly? Thanks, -- View this message in context: http://r.789695.n4.nabble.com/Cumulative-vs-non-cumulative-IRFs-in-R-tp4711138.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Bayesian data analysis recommendations
I have a question about AtelieR out on stackoverflow.com. Perhaps you could forward the link to: Yvonnick Noel University of Brittany Department of Psychology Rennes, France Here is the link: AtelieR GTK GUI http://stackoverflow.com/q/32023277/5229811?sem=2 Thank you, Jeff [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] Error - cannot open file 'specdata/001.csv': No such file or directory; Windows 8, R Version 3.2.1
This is pretty obviously difficulty in completing an assignment for a Coursera task. You are asked by the Course Director of the entry-level R course to post your questions to a website established for the purpose of requesting assistance. Rhelp is a text-only mailing list (although you might have been duped into thinking that it was a website by the Nabble mirror.) Rhelp has a no homework policy. -- David. On Aug 15, 2015, at 10:06 AM, Nikita Dinger wrote: I am having a problem in opening the excel files in specdata folder. I have completed coding the R program for the assignment but when I run the following commands in the R console, *source(pollutantmean.R)* * pollutantmean(specdata, nitrate, 23)* I get an error message stating *Error in file(file, rt) : cannot open the connection* *In addition: Warning message:* *In file(file, rt) :* * cannot open file 'specdata/023.csv': No such file or directory* I tried everything and reset my Working Directory to *C:/Users/acer/My Documents/specdata/rprog-data-specdata/specdata* After the last specdata folder are all the excel sheets numbered 001 to 332. I searched the internet and all other options available, and got a solution to open it using the following command: *df - read.csv(specdata/001.csv)* This generated the following error message *Error in file(file, rt) : cannot open the connection* *In addition: Warning message:* *In file(file, rt) :* * cannot open file 'specdata/001.csv': No such file or directory* I have tried various other commands also such as *path - c(paste(./,directory, /,formatC(id[i], width=3, flag=0),.csv,sep=))* However, all the commands show some error. What shall I do? I am using the 3.2.1 version of R on a Windows 8 laptop. Regards, Nikita Dinger [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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 Alameda, CA, USA __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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
Hi André, You have not told us how you are creating the boxplots. See http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example and http://adv-r.had.co.nz/Reproducibility.html for some suggestions on how to ask a question for the R-help list. John Kane Kingston ON Canada -Original Message- From: andrl...@ualberta.ca Sent: Fri, 14 Aug 2015 13:46:38 -0600 To: r-help@r-project.org Subject: [R] Help Dear everyone, Would like to know how to add asterisks and arcs that indicate a subgroup comparison above box plots to denote statistical diference. Thank you. Andre [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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. FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family! Visit http://www.inbox.com/photosharing to find out more! __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] minimal reproducible read.fwf() example that crashes the console on windows 8 with 32-bit R
hi, if i copy and paste this (pretty straightforward) code into R 3.2.2's 32-bit console, the program dies. if i use 64-bit R, the console doesn't die, but the process ends with a weird line-ending warning. i'm under the impression that if the console crashes, it's a bug? but i wanted to check with r-help that i'm not doing something silly before filing a formal bug report.. i get the same crash using 3.2.1 but do not need setInternet2( FALSE ) if i use 3.2.22 with setInternet2( TRUE ) then the download throws an internet connectivity error (but the console does not crash) thanks! sessionInfo() # R version 3.2.2 (2015-08-14) # Platform: i386-w64-mingw32/i386 (32-bit) # Running under: Windows 8 x64 (build 9200) # locale: # [1] LC_COLLATE=English_United States.1252 # [2] LC_CTYPE=English_United States.1252 # [3] LC_MONETARY=English_United States.1252 # [4] LC_NUMERIC=C # [5] LC_TIME=English_United States.1252 # attached base packages: # [1] stats graphics grDevices utils datasets methods base setInternet2( FALSE ) widths - c(5, 2, -3, 2, 2, 1, 1, 1, 1, 1, 1, 5, -2, 2, 1, 1, 1, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 1, 1, 2, 2, 2, 2, -1, 1, 2, 1, 2, 1, 2, 2, 1, 1, 1, 5, 1, 1, 2, 2, 1, 2, 2, 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 1, 2, 2, 1, 1, 1, 5, 1, 1, 2, 2, 1, 2, 2, 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 1, 2, 2, 1, 1, 1, 5, 1, 1, 2, 2, 1, 1, 1, 5, 5, 5, 5, 5, 5, 1, 3, 5, 5, 3, 5, 5, 3, 5, 5, 3, 5, 5, 5, 1, 1, 1, 1, 1, 1, 1, 3, 4, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 2, 2, 2, 2, 2, 7, 7, 7, 7, 7, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -2369, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8) varnames - c(SEQNUM, RECTYPE, PREG_NUM, PREGTYPE, NUMBIRTH, OUTCOME1, OUTCOME2, OUTCOME3, DELIVERY, NEWFLAG, B14MO, B_15, B_16, BOX7, B_17, B_18, B_19, B_20, B_21, B_22, B_23, B_24, B25A, B25B, B25C, B25D, B25E, B25F, B_26, B_27, B_28, B29A, B29B, B29C, B29D, B29E, B29F, B29G, B_30, BOX8, BLIVEBIR, LASTPREG, B12_1, B31LB_1, B31OZ_1, B32_1, BOX10_1, B33A_1, B33B_1, B33C_1, B33D_1, B33E_1, B33F_1, B34_1, B35_1, B36_1, B37_1, B38_1, BOX11_1, B39_1, B40_1, B41MO_1, BOX12_1, B42_1, B43_1, B44_1, B12_2, B31LB_2, B31OZ_2, B32_2, BOX10_2, B33A_2, B33B_2, B33C_2, B33D_2, B33E_2, B33F_2, B34_2, B35_2, B36_2, B37_2, B38_2, BOX11_2, B39_2, B40_2, B41MO_2, BOX12_2, B42_2, B43_2, B44_2, B12_3, B31LB_3, B31OZ_3, B32_3, BOX10_3, B33A_3, B33B_3, B33C_3, B33D_3, B33E_3, B33F_3, B34_3, B35_3, B36_3, B37_3, B38_3, BOX11_3, B39_3, B40_3, B41MO_3, BOX12_3, B42_3, B43_3, B44_3, B_45, B_46, C12A, C13F1MO, C13T1MO, C13F2MO, C13T2MO, C13F3MO, C13T3MO, C_14, C15M1, C16M1MO, C17M1MO, C15M2, C16M2MO, C17M2MO, C15M3, C16M3MO, C17M3MO, C15M4, C16M4MO, C17M4MO, C18MO, C_19, C_20, C_21, C_22, C_23, C_24, C_25, PRGLNGTH, AGEPREG, WANTWIFE, WANTMAN, OUTCOME, YRPREG, FMAROUT, LIVBABY1, LIVBABY2, LIVBABY3, LOW1, LOW2, LOW3, PREGTEST, PNCAREWK, PNCARENO, RACE, CEND84, BIRTH071, BIRTH072, BIRTH073, PREGNUM7, PREGNUM8, W_1, W_2, W_3, W_4, W_5, FLAG341, FLAG372, FLAG373, FLAG374, FLAG375, FLAG376, FLAG426, FLAG427, FLAG614, FLAG621, FLAG991, FLAG992, REPWGT1, REPWGT2, REPWGT3, REPWGT4, REPWGT5, REPWGT6, REPWGT7, REPWGT8, REPWGT9, REPWGT10, REPWGT11, REPWGT12, REPWGT13, REPWGT14, REPWGT15, REPWGT16, REPWGT17, REPWGT18, REPWGT19, REPWGT20, REPWGT21, REPWGT22, REPWGT23, REPWGT24, REPWGT25, REPWGT26, REPWGT27, REPWGT28, REPWGT29, REPWGT30, REPWGT31, REPWGT32, REPWGT33, REPWGT34, REPWGT35, REPWGT36, REPWGT37, REPWGT38, REPWGT39, REPWGT40, REPWGT41, REPWGT42, REPWGT43, REPWGT44, REPWGT45, REPWGT46, REPWGT47, REPWGT48, REPWGT49, REPWGT50, REPWGT51, REPWGT52, REPWGT53, REPWGT54, REPWGT55, REPWGT56, REPWGT57, REPWGT58, REPWGT59, REPWGT60, REPWGT61, REPWGT62, REPWGT63, REPWGT64, REPWGT65, REPWGT66, REPWGT67, REPWGT68, REPWGT69, REPWGT70, REPWGT71, REPWGT72, REPWGT73, REPWGT74, REPWGT75, REPWGT76, REPWGT77, REPWGT78, REPWGT79, REPWGT80, REPWGT81, REPWGT82, REPWGT83, REPWGT84, REPWGT85, REPWGT86, REPWGT87, REPWGT88, REPWGT89, REPWGT90, REPWGT91, REPWGT92, REPWGT93, REPWGT94, REPWGT95, REPWGT96, REPWGT97, REPWGT98, REPWGT99, REPWGT100) x - read.fwf( file = ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/Datasets/NSFG/1988PregData.dat; , widths = widths , col.names = varnames , comment.char = , colClasses = character , buffersize = 50 , n = 1000 , skip = 0 ) [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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,