Re: [R] RSQLite to input dataframe
Dear sir, When I was going through RSQLite.pdf file, I was just wondering if at all I will understand this. But you have explained it in such a simple and lucid manner, that although I am yet to go thoroughly through the mail, I am filled with confidence that yes now i can do it and I am going to circulate your mail through my office so that all will be benefited. Thanks a lot sir once again. Must say that was "The Guidnace". Regards Amy --- On Tue, 1/4/11, Michael Bedward wrote: From: Michael Bedward Subject: Re: [R] RSQLite to input dataframe To: "Amy Milano" , "Rhelp" Date: Tuesday, January 4, 2011, 11:32 AM Hi Amy, I'm not sure if I understand your question correctly so let me know if the following is off track. Starting with your example, here is how to create a data.frame and write it to a new table in a new database file... my.data = data.frame(X = c("US", "UK", "Canada", "Australia", "Newzealand"), Y = c(52, 36, 74, 10, 98)) drv <- dbDriver("SQLite") con <- dbConnect(drv, "myfilename.db") dbWriteTable(con, "sometablename", my.data) To verify that the table is now in the file... dbListTables(con) To check the fields in the table (should match the colnames in your data.frame)... dbListFields(con, "sometablename") To read the whole table into the workspace as a new data.frame my.data.copy <- dbReadTable(con, "sometablename") If you have data in a CSV file, and the contents are small enough to read in one go, you would use the read.csv function to read the contents of the file into a data.frame and then use dbWriteTable to transfer this to your database. Hope this helps, Michael On 4 January 2011 21:43, Amy Milano wrote: > Dear r helpers, > > At first, I apologize for raising a query which seems to be a stupid > interpretation on my part. I am trying to learn SQLite. > > > > Following is an example given in the RSQLite.zip file (Page # 4) > > drv <- dbDriver("SQLite") > tfile <- tempfile() > con <- dbConnect(drv, dbname = tfile) > data(USArrests) > dbWriteTable(con, "arrests", USArrests) > > > On the similar line I am trying to read my data. > > Suppose I have a dataframe as given below. > > DF = data.frame(X = c("US", "UK", "Canada", "Australia", "Newzealand"), Y = > c(52, 36, 74, 10, 98)) > > drv <- dbDriver("SQLite") > tfile <- tempfile() > con <- dbConnect(drv, dbname = tfile) > data(DF) > dbWriteTable(con, ..., ...) # Didn't know what to write here. > > I understand I have raised a query in a stupid manner. I need to understand > is there any way I can use SQLite to read > dataframe or for that matter any csv file say e.g. 'DF.csv'. > > Please enlighten me. > > Amy > > > > [[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.
[R] RSQLite to input dataframe
Dear r helpers, At first, I apologize for raising a query which seems to be a stupid interpretation on my part. I am trying to learn SQLite. Following is an example given in the RSQLite.zip file (Page # 4) drv <- dbDriver("SQLite") tfile <- tempfile() con <- dbConnect(drv, dbname = tfile) data(USArrests) dbWriteTable(con, "arrests", USArrests) On the similar line I am trying to read my data. Suppose I have a dataframe as given below. DF = data.frame(X = c("US", "UK", "Canada", "Australia", "Newzealand"), Y = c(52, 36, 74, 10, 98)) drv <- dbDriver("SQLite") tfile <- tempfile() con <- dbConnect(drv, dbname = tfile) data(DF) dbWriteTable(con, ..., ...) # Didn't know what to write here. I understand I have raised a query in a stupid manner. I need to understand is there any way I can use SQLite to read dataframe or for that matter any csv file say e.g. 'DF.csv'. Please enlighten me. Amy [[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] Fw: Re: Default Working directory on windows 7?
Suppose your working "R" directory is C:\My Working Directory I hope now that you have installed the R, you must be having "R ICON" on your desktop. Right click on this icon, then click on Properties. Once that box is opened, you will find a tab called "Start in". There within the " " type C:\My Working Directory. So it should look like "C:\My Working Directory" Then click on OK. So whenever you execute "R" henceforth, this will remain your working directory till the time you change it using above process. I hope I have addressed your query. Of course others may be having different efficient methods to do it. HTH Amy --- On Tue, 1/4/11, W Eryk Wolski wrote: From: W Eryk Wolski Subject: [R] Default Working directory on windows 7? To: r-help@r-project.org Date: Tuesday, January 4, 2011, 9:35 AM Hi, Just installed R on a new Windows 7 machine (as admin). I feel quite uncomfortable knowing that the default WD when starting R is: > getwd() [1] "C:/Windows/system32" I guess I did something wrong when installing R... How to change R's default working directory? regards Eryk __ 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] Changing column names
Please read - http://127.0.0.1:24408/library/base/html/colnames.html I am trying to give an example. I am not a programmer and I am sure the R stalwarts will have better ways to do it. # Suppose df1 = read.csv('result.csv') where (say e.g.) result.csv is as follows result.csv var1 var2 1 25 45 2 80 132 3 135 11 country = c("US", "Canada") colnames(df1) <- country HTH Amy --- On Fri, 12/31/10, Vincy Pyne wrote: From: Vincy Pyne Subject: [R] Changing column names To: r-help@r-project.org Date: Friday, December 31, 2010, 6:07 AM Dear R helpers Wish you all a very Happy and Prosperous New Year 2011. I have following query. country = c("US", "France", "UK", "NewZealand", "Germany", "Austria", "Italy", "Canada") Through some other R process, the result.csv file is generated as result.csv var1 var2 var3 var4 var5 var6 var7 var8 1 25 45 29 92 108 105 65 56 2 80 132 83 38 38 11 47 74 3 135 11 74 56 74 74 74 29 I need the country names to be column heads i.e. I need an output like > result_new US France UK NewZealand Germany Austria Italy Canada 1 25 45 29 92 108 105 65 56 2 80 132 83 38 38 11 47 74 3 135 11 74 56 74 74 74 29 The number of countries i.e. length(country) matches with total number of variables (i.e. no of columns in 'result.csv'). One way of doing this is to use country names as column names while writing the 'result.csv' file. write.csv(data.frame(US = ..., France = ...), 'result.csv', row.names = FALSE) However, the problem is I don't know in what order the country names will appear and also there could be addition or deletion of some country names. Also, if there are say 150 country names, the above way (i.e. writing.csv) of defining the column names is not practical. Basically I want to change the column heads after the 'result.csv' is generated. Kindly guide. Regards Vincy [[alternative HTML version deleted]] -Inline Attachment Follows- __ 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] Writing a single output file
Dear sir, At the outset I sincerely apologize for reverting back bit late as I was out of office. I thank you for your guidance extended by you in response to my earlier mail regarding "Writing a single output file" where I was trying to read multiple output files and create a single output date.frame. However, I think things are not working as I am mentioning below - # Your code setwd('/temp') fileNames <- list.files(pattern = "file.*.csv") input <- do.call(rbind, lapply(fileNames, function(.name) { .data <- read.table(.name, header = TRUE, as.is = TRUE) .data$file <- .name .data })) # This produces following output containing only two columns and moreover date and yield_rates are clubbed together. date.yield_rate file 1 12/23/10,5.25 file1.csv 2 12/22/10,5.19 file1.csv 3 12/23/10,4.16 file2.csv 4 12/22/10,4.59 file2.csv 5 12/23/10,6.15 file3.csv 6 12/22/10,6.41 file3.csv 7 12/23/10,8.15 file4.csv 8 12/22/10,8.68 file4.csv # and NOT the kind of output given below where date and yield_rates are different. > input date yield_rate file 1 12/23/2010 5.25 file1.csv 2 12/22/2010 5.19 file1.csv 3 12/23/2010 5.25 file2.csv 4 12/22/2010 5.19 file2.csv 5 12/23/2010 5.25 file3.csv 6 12/22/2010 5.19 file3.csv 7 12/23/2010 5.25 file4.csv 8 12/22/2010 5.19 file4.csv So when I tried following code to produce the required result, it throws me an error. require(reshape) in.melt <- melt(input, measure = 'yield_rate') > in.melt <- melt(input, measure = 'yield_rate') Error: measure variables not found in data: yield_rate # So I tried in.melt <- melt(input, measure = 'date.yield_rate') cast(in.melt, date.yield_rate ~ file) > cast(in.melt, date ~ file) Error: Casting formula contains variables not found in molten data: date # If I try to change it as cast(in.melt, date.yield_rate ~ file) # Gives following error. Error: Casting formula contains variables not found in molten data: date.yield_rate Sir, it will be a great help if you can guide me and once again sinserely apologize for reverting so late. Regards Amy --- On Thu, 12/23/10, jim holtman wrote: From: jim holtman Subject: Re: [R] Writing a single output file To: "Amy Milano" Cc: r-help@r-project.org Date: Thursday, December 23, 2010, 1:39 PM This should get you close: > # get file names > setwd('/temp') > fileNames <- list.files(pattern = "file.*.csv") > fileNames [1] "file1.csv" "file2.csv" "file3.csv" "file4.csv" > input <- do.call(rbind, lapply(fileNames, function(.name){ + .data <- read.table(.name, header = TRUE, as.is = TRUE) + # add file name to the data + .data$file <- .name + .data + })) > input date yield_rate file 1 12/23/2010 5.25 file1.csv 2 12/22/2010 5.19 file1.csv 3 12/23/2010 5.25 file2.csv 4 12/22/2010 5.19 file2.csv 5 12/23/2010 5.25 file3.csv 6 12/22/2010 5.19 file3.csv 7 12/23/2010 5.25 file4.csv 8 12/22/2010 5.19 file4.csv > require(reshape) > in.melt <- melt(input, measure = 'yield_rate') > cast(in.melt, date ~ file) date file1.csv file2.csv file3.csv file4.csv 1 12/22/2010 5.19 5.19 5.19 5.19 2 12/23/2010 5.25 5.25 5.25 5.25 > On Thu, Dec 23, 2010 at 8:07 AM, Amy Milano wrote: > Dear R helpers! > > Let me first wish all of you "Merry Christmas and Very Happy New year 2011" > > "Christmas day is a day of Joy and Charity, > May God make you rich in both" - Phillips Brooks > > ## > > > I have a process which generates number of outputs. The R code for the same > is as given below. > > for(i in 1:n) > { > write.csv(output[i], file = paste("output", i, ".csv", sep = ""), row.names = FALSE) > } > > Depending on value of 'n', I get different output files. > > Suppose n = 3, that means I am having three output csv files viz. > 'output1.csv', 'output2.csv' and 'output3.csv' > > output1.csv > date yield_rate > 12/23/2010 5.25 > 12/22/2010 5.19 > . > . > > > output2.csv > > date yield_rate > > 12/23/2010 4.16 > > 12/22/2010 4.59 > > . > > . > > output3.csv > > > date yield_rate > > > 12/23/2010 6.15
[R] Writing a single output file
Dear R helpers! Let me first wish all of you "Merry Christmas and Very Happy New year 2011" "Christmas day is a day of Joy and Charity, May God make you rich in both" - Phillips Brooks ## I have a process which generates number of outputs. The R code for the same is as given below. for(i in 1:n) { write.csv(output[i], file = paste("output", i, ".csv", sep = ""), row.names = FALSE) } Depending on value of 'n', I get different output files. Suppose n = 3, that means I am having three output csv files viz. 'output1.csv', 'output2.csv' and 'output3.csv' output1.csv date yield_rate 12/23/2010 5.25 12/22/2010 5.19 . . output2.csv date yield_rate 12/23/2010 4.16 12/22/2010 4.59 . . output3.csv date yield_rate 12/23/2010 6.15 12/22/2010 6.41 . . Thus all the output files have same column names viz. Date and yield_rate. Also, I do need these files individually too. My further requirement is to have a single dataframe as given below. Date yield_rate1 yield_rate2 yield_rate3 12/23/2010 5.25 4.16 6.15 12/22/2010 5.19 4.59 6.41 ... ... where yield_rate1 = output1$yield_rate and so on. One way is to simply create a dataframe as df = data.frame(Date = read.csv('output1.csv')$Date, yield_rate1 = read.csv('output1.csv')$yield_rate, yield_rate2 = read.csv('output2.csv')$yield_rate, yield_rate3 = read.csv('output3.csv')$yield_rate) However, the problem arises when I am not aware how many output files are there as n can be 5 or even 100. So is it possible to write some loop or some function which will enable me to read 'n' files individually and then keeping "Date" common, only pickup the yield_curve data from each output file. Thanking in advance for any guidance. Regards Amy [[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] How to arrange the data
Dear R helpers I have one data as given below. date value1 value2 value3 30-Nov-2010 100 40 61 25-Nov-2010 108 31 88 14-Sep-2010 11 180 56 I want the following output date name amount 30-Nov-2010 value1 100 30-Nov-2010 value2 40 30-Nov-2010 value3 61 25-Nov-2010 value1 108 25-Nov-2010 value2 31 25-Nov-2010 value3 88 14-Sep-2010 value1 11 14-Sep-2010 value2 180 14-Sep-2010 value3 56 I have presented here a small part of large data. I tried to convert the data into matrix, then transpose etc. but things are not working for me. Please guide Thanking in advance Amy Milano [[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] Finding root of quadratic equation
Dear Jorge Ivan Velez sir, Thanks a million for your great solution. You have made my Sunday. Thanks again. Regards Amy --- On Sun, 11/28/10, Jorge Ivan Velez wrote: From: Jorge Ivan Velez Subject: Re: [R] Finding root of quadratic equation To: "Amy Milano" Cc: "R mailing list" Date: Sunday, November 28, 2010, 8:49 AM Hi Amy, Not using animation, but this seems to work: f <- function(x) 0.0016^2 - 0.001^2*x^2 - 0.002^2*(1-x)^2 - 2*0.7*0.001*0.002*x*(1-x) curve(f, -1, 1)abline(h = 0, lty = 2, col = 'gray') uniroot(f, c(-1,1))abline(v = .3203, lty = 2, col = 2) HTH,Jorge On Sun, Nov 28, 2010 at 3:43 AM, Amy Milano <> wrote: Dear R Helpers, I need to find the root of following equation. 0.0016^2 = (0.001*x)^2 + (0.002 * (1-x))^2 + 2 * 0.7 *0.001*0.002 * x * (1-x). I had tried using "animation " package as follows. # My Code library(animation) ani.options(nmax = 500) solu = newton.method(function(x) 0.0016^2 - 0.001^2*x^2 - 0.002^2*(1-x)^2 - 2*0.7*0.001*0.002*x*(1-x), 1, c(-1,1)) solu$root # However, I am not getting the solution. Please guide. Amy [[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.
[R] Finding root of quadratic equation
Dear R Helpers, I need to find the root of following equation. 0.0016^2 = (0.001*x)^2 + (0.002 * (1-x))^2 + 2 * 0.7 *0.001*0.002 * x * (1-x). I had tried using "animation " package as follows. # My Code library(animation) ani.options(nmax = 500) solu = newton.method(function(x) 0.0016^2 - 0.001^2*x^2 - 0.002^2*(1-x)^2 - 2*0.7*0.001*0.002*x*(1-x), 1, c(-1,1)) solu$root # However, I am not getting the solution. Please guide. Amy [[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] Using tapply?
Dear Sir, Thanks a lot for your advice and surely I will be going through your suggestion as regards ?optim. In the mean time, is it possible to guide me regarding one general question. Suppose instead of using newton.method function, I am using some other function resulting in some other process and which is using say C, E, N, R only. (i.e. I am not calculating root anymore). My problem is how do I apply this function row-wise to a table. I am not sure whether I am able to put up my query properly. Basically, I need to apply the function to a table and store the result row-wise. Thanks in advance Sir. Regards Amy --- On Mon, 10/25/10, Dennis Murphy wrote: From: Dennis Murphy Subject: Re: [R] Using tapply? To: "Amy Milano" Cc: r-help@r-project.org Date: Monday, October 25, 2010, 10:47 AM Hi: I'm pretty sure that newton.method in the animation package is meant to illustrate the technique rather than to be used as an optimizer in practice. Look at ?optim; it that doesn't meet your needs, consult the Optimization Task View at CRAN, where you will find several packages, in addition to functions in the base package, devoted to function optimization. HTH, Dennis On Mon, Oct 25, 2010 at 12:18 AM, Amy Milano wrote: Dear R helpers, I am trying to calculate the Annualized Percent Rate using following R - Code. # ## R Code library(animation) # INPUT C = 25 # Loan Amount E = 2500 # Other Cost R = 6 # Interest rate r = R/1200 # Monthly interest rate q = 12 # No of Compoundings n = 20 # No of years # # COMPUTATIONS N = n * q # No of months P = ((C + E)*r*(1+r)^N) / ((1+r)^N - 1) # Monthly Payments ani.options(nmax = 500) solu = newton.method(function(a) C * (a*(1+a)^N) - P * ((1+a)^N - 1), 1, c(R - 0.25*R, R + 0.25*R)) solu$root APR = ((1+solu$root)^q-1)*100 # _ ## RESULTS P = 1808.988, APR = 6.298708 These are the values I obtain. # ## PROBLEM This R code holds or yields results if I am dealing with only one value for each of the input. However, if there are more than one input values, how do I proceed? Say, E.g. C = c(25, 40) E = c(2500, 5000) R = c(6, 6) q = c(12, 12) n = c(20, 20) So using above code, I am able to obtain the values of P as 1808.988 and 2901.546 respectively. However, using this code I am not able to obtain "solu" as defined above. I tried using the tapply instead but seems its not working. Please guide as to how do I use tapply here. Regards and thanking in advance Amy [[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.
[R] Using tapply?
Dear R helpers, I am trying to calculate the Annualized Percent Rate using following R - Code. # ## R Code library(animation) # INPUT C = 25 # Loan Amount E = 2500 # Other Cost R = 6 # Interest rate r = R/1200 # Monthly interest rate q = 12 # No of Compoundings n = 20 # No of years # # COMPUTATIONS N = n * q # No of months P = ((C + E)*r*(1+r)^N) / ((1+r)^N - 1) # Monthly Payments ani.options(nmax = 500) solu = newton.method(function(a) C * (a*(1+a)^N) - P * ((1+a)^N - 1), 1, c(R - 0.25*R, R + 0.25*R)) solu$root APR = ((1+solu$root)^q-1)*100 # _ ## RESULTS P = 1808.988, APR = 6.298708 These are the values I obtain. # ## PROBLEM This R code holds or yields results if I am dealing with only one value for each of the input. However, if there are more than one input values, how do I proceed? Say, E.g. C = c(25, 40) E = c(2500, 5000) R = c(6, 6) q = c(12, 12) n = c(20, 20) So using above code, I am able to obtain the values of P as 1808.988 and 2901.546 respectively. However, using this code I am not able to obtain "solu" as defined above. I tried using the tapply instead but seems its not working. Please guide as to how do I use tapply here. Regards and thanking in advance Amy [[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.