[R] bwlpot problems: printing, and tick labels
Two problems with the code below. A. It produces empty JPEGs. When the 'bwplot' line alone is submitted, the plot duly shows up. B. When the 'bwplot' line alone is submitted, y labels are values 1 to 6, not actual distinct values of y$maxthreads. (C. I would, of course, prefer to produce plots for all distinct values of x$maxthreads in a single swoop, on a single figure). Can anyone help? Thank you. library(lattice) options(stringsAsFactors = F) x = structure(list(mode = c(serial, parallel, parallel, parallel, serial, parallel, parallel, parallel, parallel, serial, parallel, parallel, parallel, parallel, parallel, serial, parallel, parallel, parallel, serial, parallel, parallel, parallel, parallel, serial, parallel, parallel, parallel, parallel, parallel), tasks = c(4L, 4L, 4L, 4L, 8L, 8L, 8L, 8L, 8L, 16L, 16L, 16L, 16L, 16L, 16L, 4L, 4L, 4L, 4L, 8L, 8L, 8L, 8L, 8L, 16L, 16L, 16L, 16L, 16L, 16L), maxthreads = c(0L, 1L, 2L, 4L, 0L, 1L, 2L, 4L, 8L, 0L, 1L, 2L, 4L, 8L, 16L, 0L, 1L, 2L, 4L, 0L, 1L, 2L, 4L, 8L, 0L, 1L, 2L, 4L, 8L, 16L), rep = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), task = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), time = c(1828L, 1781L, 1656L, 2171L, 3281L, 3984L, 3468L, 2921L, 4015L, 7218L, 7828L, 8093L, 6750L, 7515L, 8703L, 1734L, 2218L, 2343L, 2171L, 3468L, 5281L, 3843L, 3687L, 3734L, 7156L, 8890L, 8937L, 7343L, 7921L, 9203L), result = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), success = c(True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True)), .Names = c(mode, tasks, maxthreads, rep, task, time, result, success), class = data.frame, row.names = c(NA, -30L)) tt = unique(x$tasks) for(i in 1:length(tt)) { y = subset(x, tasks = tt[i], select = c(maxthreads, rep, time)) file = ... jpeg(file, height = 600, width = 1200) bwplot(maxthreads ~ time, data = y, ylab = maxthreads, xlab = time, ms) dev.off() } -- View this message in context: http://r.789695.n4.nabble.com/bwlpot-problems-printing-and-tick-labels-tp3478504p3478504.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] Can't merge on datetime?
x = structure(list(date = structure(list(sec = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), min = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), hour = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), mday = c(1L, 2L, 3L, 4L, 5L, 8L, 9L, 10L, 11L, 12L, 15L, 16L, 17L, 18L, 19L, 22L, 23L, 24L, 25L, 26L, 29L), mon = c(10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L), year = c(110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L), wday = c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L), yday = c(304L, 305L, 306L, 307L, 308L, 311L, 312L, 313L, 314L, 315L, 318L, 319L, 320L, 321L, 322L, 325L, 326L, 327L, 328L, 329L, 332L), isdst = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c(sec, min, hour, mday, mon, year, wday, yday, isdst), class = c(POSIXt, POSIXlt )), n = c(8, 0, 2, 6, 0, 7, 0, 1, 9, 8, 10, 9, 3, 0, 0, 0, 0, 0, 0, 0, 0)), .Names = c(date, n), row.names = c(NA, -21L ), class = data.frame) y = structure(list(date = structure(list(sec = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), min = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), hour = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), mday = c(1L, 2L, 3L, 4L, 5L, 8L, 9L, 10L, 11L, 12L, 15L, 16L, 17L, 18L, 19L, 22L, 23L, 24L, 25L, 26L, 29L), mon = c(10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L), year = c(110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L), wday = c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L), yday = c(304L, 305L, 306L, 307L, 308L, 311L, 312L, 313L, 314L, 315L, 318L, 319L, 320L, 321L, 322L, 325L, 326L, 327L, 328L, 329L, 332L), isdst = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c(sec, min, hour, mday, mon, year, wday, yday, isdst), class = c(POSIXt, POSIXlt )), m = c(114, 107, 51, 108, 0, 121, 86, 64, 108, 89, 128, 79, 70, 97, 115, 126, 72, 107, 0, 98, 76)), .Names = c(date, m ), row.names = c(NA, -21L), class = data.frame) w = merge(x, y, all.x = T, all.y = T) Error in sort.list(bx[m$xi]) : 'x' must be atomic for 'sort.list' Have you called 'sort' on a list? Converting date to Date with as.Date fixes the problem, but what if I could not ignore the time part? -- View this message in context: http://r.789695.n4.nabble.com/Can-t-merge-on-datetime-tp3165187p3165187.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] Extracting a dataframe column as a dataframe
x = structure(list(time = structure(c(1020232904.818, 1020232904.818 ), class = c(POSIXt, POSIXct), tzone = ), price = c(321, 323.5)), .Names = c(time, price), row.names = 1:2, class = data.frame) x1 = x[,c(price)] dput(x1) c(321, 323.5) Is there similar syntax that gets price as a (single-column) dataframe? (I know that I can use subset(... select = ...)).. Thank you. -- View this message in context: http://r.789695.n4.nabble.com/Extracting-a-dataframe-column-as-a-dataframe-tp3163920p3163920.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] Lost in POSIX
df = structure(list(t = structure(c(1033963406.044, 1033974144.847, + 1033988418.836), class = c(POSIXt, POSIXct))), .Names = t, row.names = c(NA, + 3L), class = data.frame) df$min = trunc(df$t,units=mins) does not work, Jeff; you will see that my original post suggests familiarity with 'trunc' :) -- View this message in context: http://r.789695.n4.nabble.com/Lost-in-POSIX-tp3052768p3163914.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] Lost in POSIX
.. One issue with the solution proposed by Jeff is that the transformed column does not have the original's type: x = structure(list(time = structure(c(1020232904.818, 1020232904.818 ), class = c(POSIXt, POSIXct), tzone = ), price = c(321, 323.5), minute = c(1020232860, 1020232860)), .Names = c(time, price, minute), row.names = 1:2, class = data.frame) minute - function(t) { d - as.POSIXlt(t, origin = as.Date(1970-01-01)) d$sec - 0 as.POSIXct(d) } x$minute = sapply(x$time, minute) head(x) time price minute 1 2002-05-01 07:01:44 321.0 1020232860 2 2002-05-01 07:01:44 323.5 1020232860 class(x.l$minute) [1] numeric -- View this message in context: http://r.789695.n4.nabble.com/Lost-in-POSIX-tp3052768p3163969.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] Lost in POSIX
df$dt - as.Date(df$t) Thank you, David, but I need a *time* value. day was a confusing special case; how about min? -- View this message in context: http://r.789695.n4.nabble.com/Lost-in-POSIX-tp3052768p3053146.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] Lost in POSIX
Nor would I call this much of an improvement in clarity... what about min? You want to know the minimum? LOL. (And apologies for the insensitivity). Thank you for help, Jeff. This works, but I am still curious to see a solution based on trunc, if anyone can find it. -- View this message in context: http://r.789695.n4.nabble.com/Lost-in-POSIX-tp3052768p3053329.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] Lost in POSIX
Can someone please fix this snippet? (i.e. append to the dataframe a column containing truncated time value)? df = structure(list(t = structure(c(1033963406.044, 1033974144.847, 1033988418.836), class = c(POSIXt, POSIXct))), .Names = t, row.names = c(NA, 3L), class = data.frame) # Try 1 df$day = trunc.POSIXt(as.POSIXlt(df$t, origin = 1970-01-01), units = day) Error in `$-.data.frame`(`*tmp*`, day, value = list(0, 0L, 0L, 7L, : replacement has 9 rows, data has 3 # Try 2 f = function(t) trunc.POSIXt(as.POSIXlt(t, origin = 1970-01-01), units = day) df$day = sapply(df$t, f) Error in `$-.data.frame`(`*tmp*`, day, value = list(sec = 0, min = 0L, : replacement has 9 rows, data has 3 -- View this message in context: http://r.789695.n4.nabble.com/Lost-in-POSIX-tp3052768p3052768.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] Failure to aggregate
g = head(x) dput(g) structure(list(price = c(500L, 500L, 501L, 501L, 500L, 501L), size = c(221000L, 2000L, 1000L, 13000L, 3000L, 3000L), src = c(R, R, R, R, R, R), t = structure(list(sec = c(24.133, 47.096, 12.139, 18.142, 10.721, 28.713), min = c(0L, 0L, 1L, 1L, 2L, 2L), hour = c(9L, 9L, 9L, 9L, 9L, 9L), mday = c(4L, 4L, 4L, 4L, 4L, 4L), mon = c(0L, 0L, 0L, 0L, 0L, 0L), year = c(105L, 105L, 105L, 105L, 105L, 105L), wday = c(2L, 2L, 2L, 2L, 2L, 2L), yday = c(3L, 3L, 3L, 3L, 3L, 3L), isdst = c(0L, 0L, 0L, 0L, 0L, 0L)), .Names = c(sec, min, hour, mday, mon, year, wday, yday, isdst), class = c(POSIXt, POSIXlt)), d = structure(list(sec = c(0, 0, 0, 0, 0, 0), min = c(0L, 0L, 0L, 0L, 0L, 0L), hour = c(0L, 0L, 0L, 0L, 0L, 0L), mday = c(4L, 4L, 4L, 4L, 4L, 4L), mon = c(0L, 0L, 0L, 0L, 0L, 0L), year = c(105L, 105L, 105L, 105L, 105L, 105L), wday = c(2L, 2L, 2L, 2L, 2L, 2L), yday = c(3L, 3L, 3L, 3L, 3L, 3L), isdst = c(0L, 0L, 0L, 0L, 0L, 0L )), .Names = c(sec, min, hour, mday, mon, year, wday, yday, isdst), class = c(POSIXt, POSIXlt)), h = structure(list(sec = c(0, 0, 0, 0, 0, 0), min = c(0L, 0L, 0L, 0L, 0L, 0L), hour = c(9L, 9L, 9L, 9L, 9L, 9L), mday = c(4L, 4L, 4L, 4L, 4L, 4L), mon = c(0L, 0L, 0L, 0L, 0L, 0L), year = c(105L, 105L, 105L, 105L, 105L, 105L), wday = c(2L, 2L, 2L, 2L, 2L, 2L), yday = c(3L, 3L, 3L, 3L, 3L, 3L), isdst = c(0L, 0L, 0L, 0L, 0L, 0L)), .Names = c(sec, min, hour, mday, mon, year, wday, yday, isdst), class = c(POSIXt, POSIXlt)), m = structure(list(sec = c(0, 0, 0, 0, 0, 0), min = c(0L, 0L, 1L, 1L, 2L, 2L), hour = c(9L, 9L, 9L, 9L, 9L, 9L), mday = c(4L, 4L, 4L, 4L, 4L, 4L), mon = c(0L, 0L, 0L, 0L, 0L, 0L), year = c(105L, 105L, 105L, 105L, 105L, 105L), wday = c(2L, 2L, 2L, 2L, 2L, 2L), yday = c(3L, 3L, 3L, 3L, 3L, 3L), isdst = c(0L, 0L, 0L, 0L, 0L, 0L )), .Names = c(sec, min, hour, mday, mon, year, wday, yday, isdst), class = c(POSIXt, POSIXlt)), s = structure(list(sec = c(24, 47, 12, 18, 10, 28), min = c(0L, 0L, 1L, 1L, 2L, 2L), hour = c(9L, 9L, 9L, 9L, 9L, 9L), mday = c(4L, 4L, 4L, 4L, 4L, 4L), mon = c(0L, 0L, 0L, 0L, 0L, 0L), year = c(105L, 105L, 105L, 105L, 105L, 105L), wday = c(2L, 2L, 2L, 2L, 2L, 2L), yday = c(3L, 3L, 3L, 3L, 3L, 3L), isdst = c(0L, 0L, 0L, 0L, 0L, 0L)), .Names = c(sec, min, hour, mday, mon, year, wday, yday, isdst), class = c(POSIXt, POSIXlt))), .Names = c(price, size, src, t, d, h, m, s), row.names = c(NA, 6L), class = data.frame) n = sqldf(select distinct h, src, count(*) from g group by h, src) Loading required package: tcltk Loading Tcl/Tk interface ... done Error in sqliteExecStatement(con, statement, bind.data) : RS-DBI driver: (error in statement: no such table: g) In addition: Warning message: In value[[3L]](cond) : RAW() can only be applied to a 'raw', not a 'double' -- View this message in context: http://r.789695.n4.nabble.com/Failure-to-aggregate-tp2528613p2533051.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] Failure to aggregate
Thanks a lot, Jim. I am not sure what difference the various POSIXes make - in the end, you are replacing a datetime hour with a numeric value, e.g., 1 or 9. That does not work for me, unfortunately. g = head(x) dput(g) structure(list(price = c(500L, 500L, 501L, 501L, 500L, 501L), size = c(221000L, 2000L, 1000L, 13000L, 3000L, 3000L), src = c(R, R, R, R, R, R), t = structure(c(1104829224.133, 1104829247.096, 1104829272.139, 1104829278.142, 1104829330.721, 1104829348.713), class = c(POSIXt, POSIXct), tzone = ), d = structure(list(sec = c(0, 0, 0, 0, 0, 0), min = c(0L, 0L, 0L, 0L, 0L, 0L), hour = c(0L, 0L, 0L, 0L, 0L, 0L), mday = c(4L, 4L, 4L, 4L, 4L, 4L), mon = c(0L, 0L, 0L, 0L, 0L, 0L), year = c(105L, 105L, 105L, 105L, 105L, 105L), wday = c(2L, 2L, 2L, 2L, 2L, 2L), yday = c(3L, 3L, 3L, 3L, 3L, 3L), isdst = c(0L, 0L, 0L, 0L, 0L, 0L)), .Names = c(sec, min, hour, mday, mon, year, wday, yday, isdst), class = c(POSIXt, POSIXlt)), h = c(9, 9, 9, 9, 9, 9), m = structure(list( sec = c(0, 0, 0, 0, 0, 0), min = c(0L, 0L, 1L, 1L, 2L, 2L), hour = c(9L, 9L, 9L, 9L, 9L, 9L), mday = c(4L, 4L, 4L, 4L, 4L, 4L), mon = c(0L, 0L, 0L, 0L, 0L, 0L), year = c(105L, 105L, 105L, 105L, 105L, 105L), wday = c(2L, 2L, 2L, 2L, 2L, 2L), yday = c(3L, 3L, 3L, 3L, 3L, 3L), isdst = c(0L, 0L, 0L, 0L, 0L, 0L)), .Names = c(sec, min, hour, mday, mon, year, wday, yday, isdst), class = c(POSIXt, POSIXlt)), s = structure(list(sec = c(24, 47, 12, 18, 10, 28), min = c(0L, 0L, 1L, 1L, 2L, 2L), hour = c(9L, 9L, 9L, 9L, 9L, 9L), mday = c(4L, 4L, 4L, 4L, 4L, 4L), mon = c(0L, 0L, 0L, 0L, 0L, 0L), year = c(105L, 105L, 105L, 105L, 105L, 105L), wday = c(2L, 2L, 2L, 2L, 2L, 2L), yday = c(3L, 3L, 3L, 3L, 3L, 3L), isdst = c(0L, 0L, 0L, 0L, 0L, 0L)), .Names = c(sec, min, hour, mday, mon, year, wday, yday, isdst ), class = c(POSIXt, POSIXlt))), .Names = c(price, size, src, t, d, h, m, s), row.names = c(NA, 6L ), class = data.frame) n = sqldf(select distinct h, src, count(*) from g group by h, src) Error in sqliteExecStatement(con, statement, bind.data) : RS-DBI driver: (error in statement: no such table: g) In addition: Warning message: In value[[3L]](cond) : RAW() can only be applied to a 'raw', not a 'double' -- View this message in context: http://r.789695.n4.nabble.com/Failure-to-aggregate-tp2528613p2533189.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] Failure to aggregate
Update: What did make a difference for me - and something that was present in Jim's example, but not reproduced by myself initially - was dropping columns other than the two involved. When I dropped all columns except for h and src, the sqldf call worked. ... Is it an R bug or what? (I am saying R, not sqldf, because built-in tapply did not work either). Arrgh... -- View this message in context: http://r.789695.n4.nabble.com/Failure-to-aggregate-tp2528613p2533206.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] Failure to aggregate
Ok, conversion to POSIXct does the trick - why doesn't tapply work with the other, not-obviously-improper POSIX type? (Incidentally, now it gives me more trouble, with sorting - a reproducible sample coming up in another thread). -- View this message in context: http://r.789695.n4.nabble.com/Failure-to-aggregate-tp2528613p2533311.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] Failure to aggregate
Mnay thanks fr suggestions. I am afraid this is one tough daatframe... t = sqldf(select h, count(*) from x group by h) Error in sqliteExecStatement(con, statement, bind.data) : RS-DBI driver: (error in statement: no such table: x) In addition: Warning message: In value[[3L]](cond) : RAW() can only be applied to a 'raw', not a 'double' t = aggregate(x[price], by = x[h], FUN = NROW) Error in sort.list(y) : 'x' must be atomic for 'sort.list' Have you called 'sort' on a list? t = aggregate(x[price], by = x[h], FUN = length) Error in sort.list(y) : 'x' must be atomic for 'sort.list' Have you called 'sort' on a list? t = tapply(x$price, by = x$h, FUN = length) Error in is.list(INDEX) : 'INDEX' is missing class(x) [1] data.frame class(x$h) [1] POSIXt POSIXlt class(x$price) [1] integer -- View this message in context: http://r.789695.n4.nabble.com/Failure-to-aggregate-tp2528613p2530963.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] Failure to aggregate
I was able to aggregate (with sqldf, at least), after saving and re-loading the dataframe. My first guess was that h (and/or price?) now being a factor - stringsAsFactors = T by default - made the difference, and I tried to convert x$h to factor, but received an error. -- View this message in context: http://r.789695.n4.nabble.com/Failure-to-aggregate-tp2528613p2530989.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] Failure to aggregate
I have a (very big - 1.5 rows) dataframe with a (POSIXt POSIXlt) column h (hour). Surprisingly, I cannot calculate a simple aggregate over the dataframe. n.h1 = sqldf(select distinct h, count(*) from x group by h) Error in sqliteExecStatement(con, statement, bind.data) : RS-DBI driver: (error in statement: no such table: x) In addition: Warning message: In value[[3L]](cond) : RAW() can only be applied to a 'raw', not a 'double' n.h2 = aggregate(x$price, by = x$h, FUN = nrow) Error in names(y) - c(names(by), names(x)) : 'names' attribute [10] must be the same length as the vector [2] Arrgh... -- View this message in context: http://r.789695.n4.nabble.com/Failure-to-aggregate-tp2528613p2528613.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] Quick GREP challenge
grep(f[0-9]+=, f1=5,f22=3,, value = T) [1] f1=5,f22=3, How do I make the line output c(f1, f22) instead? (Actually, c(1,22) would be even better). Thank you. -- View this message in context: http://r.789695.n4.nabble.com/Quick-GREP-challenge-tp2339486p2339486.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] Quick GREP challenge
Many thanks! -- View this message in context: http://r.789695.n4.nabble.com/Quick-GREP-challenge-tp2339486p2339818.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] Code to calculate internal rate of return
Unfortunately, this is not a robust implementation, and is derailed by Error in uniroot(f, c(-1, 1)) : f() values at end points not of opposite sign -- View this message in context: http://r.789695.n4.nabble.com/Code-to-calculate-internal-rate-of-return-tp864645p2231910.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] How to read contents of a text file into a single string?
... Both readLines() and scan() produce a number_of_lines x 1 vector; trying paste(s, collapse = NULL) leaves it unaffected. How can I concatenate vector elements (lines) into a single string? Thank you. -- View this message in context: http://r.789695.n4.nabble.com/How-to-read-contents-of-a-text-file-into-a-single-string-tp2069303p2069303.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] Reading a password-protected Excel workbook
I try reading a password-protected spreadsheet with RODBC, passing a file or an ODBC source to odbcConnect, e.g. con = odbcConnect(dsn = mywkbk, uid = , pwd = mypwd) but get Could not decrypt file pop-up error message. Can anyone help? Thank you. -- View this message in context: http://n4.nabble.com/Reading-a-password-protected-Excel-workbook-tp1568925p1568925.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] Newbie woes with par:mar
I have a simple barchart with horizontal bars and horizontal tick labels, produced with barplot(x, horiz = T, names.arg = c, las = 1) The labels are longish strings, truncated on the plot. I wish to leave more space for the left margin, and experiment with mar parameter, barplot(x, horiz = T, names.arg = c, las = 1, mar = c(5, 15, 4, 2)) trying various values for the second vector element, but do not notice any change. Consulting this nice tutorial http://research.stowers-institute.org/efg/R/Graphics/Basics/mar-oma/index.htm has not helped. Can anyone point me in the right direction? Thank you. -- View this message in context: http://n4.nabble.com/Newbie-woes-with-par-mar-tp1563745p1563745.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] Newbie woes with par:mar
Thanks a lot, Peter! -- View this message in context: http://n4.nabble.com/Newbie-woes-with-par-mar-tp1563745p1563905.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] Problems with boxplot in ggplot2:qplot
Thanks a lot, Brian! -- View this message in context: http://n4.nabble.com/Problems-with-boxplot-in-ggplot2-qplot-tp1555338p1558810.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] Problems with boxplot in ggplot2:qplot
Now that we have a reproducible example... ;) -- View this message in context: http://n4.nabble.com/Problems-with-boxplot-in-ggplot2-qplot-tp1555338p1557994.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] Problems with boxplot in ggplot2:qplot
Thank you, Hadley. I try jpeg(file, width = 800, height = 600, quality = 100) qplot(factor(closed$close.month), closed$closing.balance, geom = boxplot, main = Monthly distributions of closing balances, xlab = Month, ylab = Balance, USD) + theme_bw() + scale_x_date(major = months, minor = weeks, format = %b) dev.off() ('minor = ' can be skipped with no consequences, apparently). Labels disappear altogether. -- View this message in context: http://n4.nabble.com/Problems-with-boxplot-in-ggplot2-qplot-tp1555338p1556571.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] Problems with boxplot in ggplot2:qplot
Trying + scale_x_date(format = %b) produces a peculiar result: Apr and Dec facets are labeled Jan, remaining labels are blank. -- View this message in context: http://n4.nabble.com/Problems-with-boxplot-in-ggplot2-qplot-tp1555338p1556573.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] Problems with boxplot in ggplot2:qplot
library(sqldf) library(ggplot2) t = data.frame(t = seq.Date(as.Date(2009-01-01), to = as.Date(2009-12-01), by = month)) x = data.frame(x = rnorm(5)) df = sqldf(select * from t, x) qplot(factor(df$t), df$x, geom = boxplot) + theme_bw() qplot(factor(df$t), df$x, geom = boxplot) + theme_bw() + scale_x_date(major = months, minor = weeks, format = %b) qplot(factor(df$t), df$x, geom = boxplot) + theme_bw() + scale_x_date(format = %b) -- View this message in context: http://n4.nabble.com/Problems-with-boxplot-in-ggplot2-qplot-tp1555338p1556745.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] Newbie woes with *apply
Bug fix: first.day.of.quarter = function(date) { t = first.day.of.month(date) l = month(date) %% 3 if (l == 0) return(t) t = seq.Date(t, by = -1 month, length = l) return(t[length(t)]) } But the *apply part still does not work. -- View this message in context: http://n4.nabble.com/Newbie-woes-with-apply-tp1555149p1555167.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] Newbie woes with *apply
Dataframe cust has Date-type column open.date. I wish to set up another column, with (first day of) the quarter of open.date. To be comprehensive (of course, improvement suggestions are welcome), month = function(date) { return(as.numeric(format(date,%m))) } first.day.of.month = function(date) { return(date + 1 - as.numeric(format(date,%d))) } first.day.of.quarter = function(date) { t = seq.Date(first.day.of.month(date), by = -1 month, length = month(date) %% 3) return(t[length(t)]) } Now the main part, cust$open.quarter = apply(cust$open.date, 1, FUN = first.day.of.quarter) Error in apply(cust$open.date, 1, FUN = first.day.of.quarter) : dim(X) must have a positive length cust$open.quarter = tapply(cust$open.date, FUN = first.day.of.quarter) Error in tapply(cust$open.date, FUN = first.day.of.quarter) : element 1 is empty; the part of the args list of 'is.list' being evaluated was: (INDEX) cust$open.quarter = lapply(cust$open.date, FUN = first.day.of.quarter) Error in prettyNum(.Internal(format(x, trim, digits, nsmall, width, 3L, : invalid 'trim' argument Can anyone suggest the right syntax? Thank you. -- View this message in context: http://n4.nabble.com/Newbie-woes-with-apply-tp1555149p1555149.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] Newbie woes with *apply
Many thanks, but my focus is actually on *apply usage. -- View this message in context: http://n4.nabble.com/Newbie-woes-with-apply-tp1555149p1555329.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] Problems with boxplot in ggplot2:qplot
Dataframe closed contains balances of closed accounts: each row has month of closure (Date-type column month) and latest balance. I would like to plot by-month distributions of balances. A qplot call below produces several warnings and no output. Can anyone help? Thank you. PS. A really basic task, very similar to the examples on p. 71 of the ggplot2 book, apart from a Date grouping column; I am quite surprised to have problems with it. lattice package to the rescue? qplot(factor(month), balance, data = closed, geom = boxplot, xlim = range(closed$month)) There were 13 warnings (use warnings() to see them) warnings() Warning messages: 1: Removed 1 rows containing missing values (stat_boxplot). 2: Removed 7 rows containing missing values (geom_point). 3: Removed 5 rows containing missing values (geom_point). 4: Removed 8 rows containing missing values (geom_point). 5: Removed 3 rows containing missing values (geom_point). 6: Removed 5 rows containing missing values (geom_point). 7: Removed 2 rows containing missing values (geom_point). 8: Removed 12 rows containing missing values (geom_point). 9: Removed 2 rows containing missing values (geom_point). 10: Removed 1 rows containing missing values (geom_point). 11: Removed 2 rows containing missing values (geom_point). 12: Removed 3 rows containing missing values (geom_point). 13: Removed 4 rows containing missing values (geom_point). p = qplot(factor(month), balance, data = closed, geom = boxplot, xlim = range(closed$month)) plot(p) Error in plot.window(...) : need finite 'xlim' values -- View this message in context: http://n4.nabble.com/Problems-with-boxplot-in-ggplot2-qplot-tp1555338p1555338.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] Problems with boxplot in ggplot2:qplot
... Unfortunately, a problem remains: I cannot label x ticks a la 'names.arg = '. month has values like '2009-01-01', '2009-02-01', etc., while I would prefer 'Jan', 'Feb'. Using closed$month = format(closed$month, %b) disrupts the order of plot's panels, which now follows the alphabetic order of month names. -- View this message in context: http://n4.nabble.com/Problems-with-boxplot-in-ggplot2-qplot-tp1555338p1555358.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] Problems with boxplot in ggplot2:qplot
My bad: once I ran dev.off(), I did get a plot, albeit a blank one. Then I removed xlim - which I put in after qplot's complain about xlim - and voila! Thanks a lot. -- View this message in context: http://n4.nabble.com/Problems-with-boxplot-in-ggplot2-qplot-tp1555338p1555352.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] Suppressing scientific notation on plot axis tick labels
Is there a better alternative to x = c(1e7, 2e7) x.lb = c(0,1e7,2e7) s.lb = format(x.lb, scientific = FALSE, big.mark = ,) barplot(x, yaxt = n, ylab = ) axis(side = 2, at = x.lb, labels = s.lb) (I am sure there is a better alternative to line 2 :)). Thank you. -- View this message in context: http://n4.nabble.com/Suppressing-scientific-notation-on-plot-axis-tick-labels-tp1459697p1459697.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] Suppressing scientific notation on plot axis tick labels
Ruben Roa has kindly suggested using 'scipen' option - cf. fixed notation will be preferred unless it is more than ‘scipen’ digits wider. However, options(scipen = 50) x = c(1e7, 2e7) barplot(x) still does not produce the desired result. -- View this message in context: http://n4.nabble.com/Suppressing-scientific-notation-on-plot-axis-tick-labels-tp1459697p1459789.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] Suppressing scientific notation on plot axis tick labels
options(scipen = 50, digits = 5) x = c(1e7, 2e7) barplot(x) Still scientific... -- View this message in context: http://n4.nabble.com/Suppressing-scientific-notation-on-plot-axis-tick-labels-tp1459697p1459828.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] Solving an optimization problem: selecting an optimal subset
Thank you very much, Erwin. If I may ask some follow-up questions 1. GAMS R, ad it's just not entirely clear how to move the soltion to R. (At most trivial, how do I bring in the subsettable vector into the solver?) 2. The quadratic objective can be replaced by a linear one by minimizing the absolute deviation. Minimizing absolute deviation is not, as far as I can see, a linear problem... ?? What magic is happening in these lines? positive variable d1,d2; e3.. d1-d2 =e= s-target; obj.. z =e= d1+d2; -- View this message in context: http://n4.nabble.com/Solving-an-optimization-problem-selecting-an-optimal-subset-tp1446084p1457983.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] Solving an optimization problem: selecting an optimal subset
To replace an absolute value by two binary variables is an old trick in optimization modeling. I am having trouble Googling it: can anyone suggest a reference, or just explain? Thank you. -- View this message in context: http://n4.nabble.com/Solving-an-optimization-problem-selecting-an-optimal-subset-tp1446084p1457988.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 choose a number subset with sum closest to x?
Oh, I have considered this, but just don't see how the problem can be cast as LSAP. (In the example, sum of squared pairwise distances ('costs') is minimized, whereas here I am squaring the distance of a sum from the target number. (How do I define individual assignment's cost here?) Thanks anyway! I will be sure to study the (longer) code snippet in that post. PS. Sorry, my post became duplicated. -- View this message in context: http://n4.nabble.com/How-to-choose-a-number-subset-with-sum-closest-to-x-tp1457245p1457354.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] Solving an optimization problem: selecting an optimal subset
Is it a subset of a vector containing 100 elements, or 1ths? I need to pick 2-40 elements out of a 50-200-element-long vector. A random number of elements that should be chosen, or the best 10 values which sums up to a defined value? The best 10 values. I still think that Rcplex is the way to go; what's missing is some linear-algebra expertise on my part to set up the problem as quadratic. -- View this message in context: http://n4.nabble.com/Solving-an-optimization-problem-selecting-an-optimal-subset-tp1446084p1457355.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] Solving an optimization problem: selecting an optimal subset
This is a subset sum problem and has been discussed here in December Thanks a lot! Will investigate. Can you settle for an approximate solution? Absolutely. Rcplex: This is a combinatorial problem and cannot be formulated as a quadratic optimization problem. If the objective function can fit the pattern, we need to find the set of n coefficients, taking values 0 or 1, summing to m, for the m-out-of-n problem. 'Binary' version of Rcplex apparently would be able to handle that. It is NP-hard and cannot be solved via Dynamic Programming. Why not? Discretize the [0, sum(x)] range and solve an m-step DP problem. The value function would minimize the distance from s, and penalize too-short (m* m) subsets. Thanks again! -- View this message in context: http://n4.nabble.com/Solving-an-optimization-problem-selecting-an-optimal-subset-tp1446084p1457390.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] Solving an optimization problem: selecting an optimal subset
Found this http://n4.nabble.com/Subset-sum-problem-td954423.html#a954423 http://n4.nabble.com/The-subset-matching-challenge-td861840.html#a861840 and learnt/remebered about 'subset sum' and 'knapsack' problems. My case is different (simpler) in that subset size is fixed. -- View this message in context: http://n4.nabble.com/Solving-an-optimization-problem-selecting-an-optimal-subset-tp1446084p1457395.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] Applying a transformation to multiple data frame columns
How can one simplify the folowing? t$aum[is.na(t$aum)] = 0; t$aum.core[is.na(t$aum.core)] = 0 t$num[is.na(t$num)] = 0; t$num.core[is.na(t$num.core)] = 0 Thank you. -- View this message in context: http://n4.nabble.com/Applying-a-transformation-to-multiple-data-frame-columns-tp1457641p1457641.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] Solving an optimization problem: selecting an optimal subset
A 40-element subset proves too much :( Error: cannot allocate vector of size 554.1 Mb Thanks, Bart! -- View this message in context: http://n4.nabble.com/Solving-an-optimization-problem-selecting-an-optimal-subset-tp1446084p1457597.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] Solving an optimization problem: selecting an optimal subset
?!! Erwin, may I ask for a working code sample? (Including appropriate package(s)) Thank you. -- View this message in context: http://n4.nabble.com/Solving-an-optimization-problem-selecting-an-optimal-subset-tp1446084p1457746.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] Solving an optimization problem: selecting an optimal subset
Same request to Hans: I am afraid I need a little more spoon-feeding following I sent a GAMS script modeling this problem to the NEOS solvers Thanks a lot! -- View this message in context: http://n4.nabble.com/Solving-an-optimization-problem-selecting-an-optimal-subset-tp1446084p1457747.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] How to choose a number subset with sum closest to x?
I am looking for a function that, when supplied a vector of floats x, an integer n, and float s, would find me an n-subset of x with the sum closest to s. Can anyone point me to a package/function that can do the job - better yet, provide a relevant code sample? (There are a few relevant packages on CRAN, but lack of examples in their docs is a real problem). Thank you. -- View this message in context: http://n4.nabble.com/How-to-choose-a-number-subset-with-sum-closest-to-x-tp1439939p1439939.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] Solving an optimization problem: selecting an optimal subset
Given vector of numbers x, I wish to select an n-subset with sum closest to fixed value s. Can anyone advise me how to approach this, in R? I have considered Rcplex package, which handles integer/binary linear/quadratic optimization problems, but have difficulty setting up the quadratic form for [sum(x) - s]^2. (Dynamic programming over [0, sum(x)]? A genetic algorithm? Can anyone contribute a binary GA optimization sample?) Thank you. -- View this message in context: http://n4.nabble.com/Solving-an-optimization-problem-selecting-an-optimal-subset-tp1446084p1446084.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 implement a select distinct x, count(distinct y) ... group by x for a data frame
sqldf answered my prayers and more. Thanks a lot! -- View this message in context: http://n4.nabble.com/How-to-implement-a-select-distinct-x-count-distinct-y-group-by-x-for-a-data-frame-tp1288418p1288489.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] Is there a quicker way to drop a data frame column than setting it to NULL?
If I want to drop columns x, y, z from dataframe df, is there a better alternative to df$x = NULL df$y = NULL df$z = NULL There are sufficiently many columns remaining to make df = subset(df, select = c(a,b,c,d[etc])) cumbersome. Thank you. -- View this message in context: http://n4.nabble.com/Is-there-a-quicker-way-to-drop-a-data-frame-column-than-setting-it-to-NULL-tp1288617p1288617.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] How to implement a select distinct x, count(distinct y) ... group by x for a data frame
... Being an R newbie, I can only think of extracting distinct x values with unique, looping over them, extracting matching rows from the original data frame, applying table, and recording the size of table's output alongside the x value being checked. Is there a more elegant way? Thank you. -- View this message in context: http://n4.nabble.com/How-to-implement-a-select-distinct-x-count-distinct-y-group-by-x-for-a-data-frame-tp1288418p1288418.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] exporting text output to pdf
The modified snippet (with the long paragraph truncated) does not produce anything like the requested document. -- View this message in context: http://n4.nabble.com/exporting-text-output-to-pdf-tp837699p1017332.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] exporting text output to pdf
Thanks a lot! -- View this message in context: http://n4.nabble.com/exporting-text-output-to-pdf-tp837699p1017331.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] exporting text output to pdf
Ah, mine was not a helpful example. Text atop a picture is a special scenario, handled by title(); the goal is to produce a proper-looking PDF *document* with text and images - similar to what I could do in HTML with hwriter package. -- View this message in context: http://n4.nabble.com/exporting-text-output-to-pdf-tp837699p1016744.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] exporting text output to pdf
Nothing relevant, as far as I can tell :( Putting text into a PDF document in R appears to be a cumbersome exercise. -- View this message in context: http://n4.nabble.com/exporting-text-output-to-pdf-tp837699p1016808.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] exporting text output to pdf
Let's revisit my fist post How can I get a normal-font-sized 'Hello world' displayed on a page, with a plot underneath, instead of a giant 'Hello world' on one page, and a plot on another? Then The goal is to produce a proper-looking PDF *document* with text and images - similar to what I could do in HTML with hwriter package. Document as opposed to annotated plot. There is the difficulty of defining, specifically, a 'proper document', and not everyone is familiar with hwriter. How about this: I want an interface in which commands like page = pdf(..) print(page, some text) printImage(page, someimage.jpg) close(page) would produce a one-page PDF document with a line of text and a picture. -- View this message in context: http://n4.nabble.com/exporting-text-output-to-pdf-tp837699p1016930.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] exporting text output to pdf
Error in library(ReadImages) : .First.lib failed for 'ReadImages' David, I was aware of title, main and mtext when I asked the question, and although advice concerning them was certainly sound, it was not news. Yes, one can put text into images - I wish I had not brought up images at all and focused on text - and then embed images in a PDF (hopefully, without page breaks), but laying text out this way - i.e. creating a 'proper-looking document' - would be a very painful exercise, would you agree? -- View this message in context: http://n4.nabble.com/exporting-text-output-to-pdf-tp837699p1017074.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] exporting text output to pdf
I wish I had not brought up images at all and focused on text In fact, let's do that and forget about images - I just want to put text into a PDF. Let's take, say, five long strings to represent paragraphs, then put them on a single page, one after another, with minimal code. I don't want to fiddle with line breaks and 'layout', and would prefer being able to put the text together by paragraph, rather than in one fell swoop. -- View this message in context: http://n4.nabble.com/exporting-text-output-to-pdf-tp837699p1017082.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] exporting text output to pdf
... You can modify this (dysfunctional) snippet. pdf() plot.new() mtext(Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque leo ipsum, ultricies scelerisque volutpat non, volutpat et nulla. Curabitur consequat ullamcorper tellus id imperdiet. Duis semper malesuada nulla, blandit lobortis diam fringilla at. Vestibulum nec tellus orci, eu sollicitudin quam. Phasellus sit amet enim diam. Phasellus mattis hendrerit varius. Curabitur ut tristique enim. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed convallis, tortor id vehicula facilisis, nunc justo facilisis tellus, sed eleifend nisi lacus id purus. Maecenas tempus sollicitudin libero, molestie laoreet metus dapibus eu. Mauris justo ante, mattis et pulvinar a, varius pretium eros. Curabitur fringilla dui ac dui rutrum pretium. Donec sed magna adipiscing nisi accumsan congue sed ac est. Vivamus lorem urna, tristique quis accumsan quis, ullamcorper aliquet velit.) mtext(A nice-looking paragraph! Now this is what I call good advice!) dev.off() -- View this message in context: http://n4.nabble.com/exporting-text-output-to-pdf-tp837699p1017087.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.