Re: [R] Split a DF on Date column for each single year
In your sample data.frame, MyDate and MyDes are factors; is that what you want? rs On Samstag, 16. März 2019 01:40:01 CET Ek Esawi wrote: > Hi All— > > I have a data frame with over 13000 rows and 4 columns. A mini data > frame is given at the bottom. I want to split the data frame into > lists each corresponds to single year which ranges from 1990 to 2018). > I wanted to use the split function, but it requires a vector of the > same length as MyDate which contains many multiples of each year. > Any help is highly appreciated. > > I want the following results: > List 1990 > MyDate MyNo MyDes > 1990 > 1990 > 1990 > …... > List 2000 > 2000 > 2000 > 2000 > …... > List 2001 > 2001 > 2001 > 2001 > 2001 > …... > List 2018 > 2018 > 2018 > 2018 > …... > > Sample data frame > > mydf <- > data.frame(MyDate=c("1990-01-01","1990-04-07","2000-04-05","2018-01-04"),MyNo=c(1,2,3,4),MyDes=c("AA","BB","CC","DD")) > > > EK > > __ > 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.
Re: [R] Removing a data subset
Reading in the data from the file x <- read.csv( "ExampleData.csv", header = TRUE, stringsAsFactors = FALSE ) Subsetting as you want x <- x[ x$Location != "MW01", ] This selects all rows where the value in column 'Location' is not equal to "MW01". The comma after that ensures that all columns are copied into the amended data.frame. Rgds, Rainer On Mittwoch, 29. November 2017 15:07:34 +08 David Doyle wrote: > Say I have a dataset that looks like > > LocationYear GW_Elv > MW011999 546.63 > MW021999 474.21 > MW031999 471.94 > MW041999466.80 > MW012000545.90 > MW022000546.10 > > The whole dataset is at http://doylesdartden.com/ExampleData.csv > and I use the code below to do the graph but I want to do it without MW01. > How can I remove MW01?? > > I'm sure I can do it by SubSeting but I can not figure out how to do it. > > Thank you > David > > -- > > library(ggplot2) > > MyData <- read.csv("http://doylesdartden.com/ExampleData.csv;, header=TRUE, > sep=",") > > > > #Sets whic are detections and nondetects > MyData$Detections <- ifelse(MyData$D_GW_Elv ==1, "Detected", "NonDetect") > > #Removes the NAs > MyDataWONA <- MyData[!is.na(MyData$Detections), ] > > #does the plot > p <- ggplot(data = MyDataWONA, aes(x=Year, y=GW_Elv , col=Detections)) + > geom_point(aes(shape=Detections)) + > > ##sets the colors > scale_colour_manual(values=c("black","red")) + #scale_y_log10() + > > #location of the legend > theme(legend.position=c("right")) + > > #sets the line color, type and size > geom_line(colour="black", linetype="dotted", size=0.5) + > ylab("Elevation Feet Mean Sea Level") > > ## does the graph using the Location IDs as the different Locations. > p + facet_grid(Location ~ .) > > [[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.
Re: [R] [FORGED] Re: remove
I may not be understanding the question well enough but for me df[ df[ , "first"] != "Alex", ] seems to do the job: first week last Rainer On Sonntag, 12. Februar 2017 19:04:19 CET Rolf Turner wrote: > > On 12/02/17 18:36, Bert Gunter wrote: > > Basic stuff! > > > > Either subscripting or ?subset. > > > > There are many good R tutorials on the web. You should spend some > > (more?) time with some. > > Uh, Bert, perhaps I'm being obtuse (a common occurrence) but it doesn't > seem basic to me. The only way that I can see how to go at it is via > a for loop: > > rdln <- function(X) { > # Remove discordant last names. > ok <- logical(nrow(X)) > for(nm in unique(X$first)) { > xxx <- unique(X$last[X$first==nm]) > if(length(xxx)==1) ok[X$first==nm] <- TRUE > } > Y <- X[ok,] > Y <- Y[order(Y$first),] > rownames(Y) <- 1:nrow(Y) > Y > } > > Calling the toy data frame "melvin" rather than "df" (since "df" is the > name of the built in F density function, it is bad form to use it as the > name of another object) I get: > > > rdln(melvin) >first week last > 1 Bob1 John > 2 Bob2 John > 3 Bob3 John > 4 Cory1 Jack > 5 Cory2 Jack > > which is the desired output. If there is a "basic stuff" way to do this > I'd like to see it. Perhaps I will then be toadally embarrassed, but > they say that this is good for one. > > cheers, > > Rolf > > > On Sat, Feb 11, 2017 at 9:02 PM, Valwrote: > >> Hi all, > >> I have a big data set and want to remove rows conditionally. > >> In my data file each person were recorded for several weeks. Somehow > >> during the recording periods, their last name was misreported. For > >> each person, the last name should be the same. Otherwise remove from > >> the data. Example, in the following data set, Alex was found to have > >> two last names . > >> > >> Alex West > >> Alex Joseph > >> > >> Alex should be removed from the data. if this happens then I want > >> remove all rows with Alex. Here is my data set > >> > >> df <- read.table(header=TRUE, text='first week last > >> Alex1 West > >> Bob 1 John > >> Cory1 Jack > >> Cory2 Jack > >> Bob 2 John > >> Bob 3 John > >> Alex2 Joseph > >> Alex3 West > >> Alex4 West ') > >> > >> Desired output > >> > >> first week last > >> 1 Bob 1 John > >> 2 Bob 2 John > >> 3 Bob 3 John > >> 4 Cory 1 Jack > >> 5 Cory 2 Jack > > __ > 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. > [[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] Problem with X11
Probably wrong list, but anyway: Same problem here, after a apt-get dist-upgrade On Tuesday, April 19, 2016 05:23:37 PM Lorenzo Isella wrote: > Dear All, > I have never had this problem before. I run debian testing on my box > and I have recently update my R environment. > Now, see what happens when I try the most trivial of all plots > > > plot(seq(22)) > Error in (function (display = "", width, height, pointsize, gamma, bg, > : > X11 module cannot be loaded > In addition: Warning message: > In (function (display = "", width, height, pointsize, gamma, bg, : > unable to load shared object '/usr/lib/R/modules//R_X11.so': > /usr/lib/x86_64-linux-gnu/libpng12.so.0: version `PNG12_0' not > found (required by /usr/lib/R/modules//R_X11.so) > > and this is my sessionInfo() > > > sessionInfo() > R version 3.2.4 Revised (2016-03-16 r70336) > Platform: x86_64-pc-linux-gnu (64-bit) > Running under: Debian GNU/Linux stretch/sid > > locale: > [1] LC_CTYPE=en_GB.utf8 LC_NUMERIC=C > [3] LC_TIME=en_GB.utf8LC_COLLATE=en_GB.utf8 >[5] LC_MONETARY=en_GB.utf8LC_MESSAGES=en_GB.utf8 > [7] LC_PAPER=en_GB.utf8 LC_NAME=C > [9] LC_ADDRESS=C LC_TELEPHONE=C > [11] LC_MEASUREMENT=en_GB.utf8 LC_IDENTIFICATION=C > > attached base packages: > [1] stats graphics grDevices utils datasets methods base > > > Anybody understands what is going on here? > Regards > > Lorenzo > > __ > 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.
Re: [R] Existence of an object
Quotation marks help also for exists(): exists( "meanedf" ) [1] TRUE On Saturday 07 November 2015 04:48:04 Margaret Donald wrote: > I have a variable meanedf which may sometimes not be defined due to a > complex set of circumstances. > I would like to be able to find out whether or not it exists, and then > branch appropriately in my code. > > I had hoped to be able to use is.null() or exists() but neither of these > returns TRUE or FALSE which is the behaviour I would like, so that I can > write some appropriate branching code. > > is.null(meanedf) > Error: object 'meanedf' not found > No suitable frames for recover() > > exists(meanedf) > Error in exists(meanedf) : object 'meanedf' not found > > I would like some code which returns TRUE when meanedf exists and FALSE > when it doesn't. > > Thanks, and regards, > Margaret Donald > > > __ 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] Existence of an object
For me, "meanedf" %in% ls() works: meanedf <- 1 "meanedf" %in% ls() [1] TRUE rm( meanedf ) "meanedf" %in% ls() [1] FALSE Rgds, Rainer On Saturday 07 November 2015 04:48:04 Margaret Donald wrote: > I have a variable meanedf which may sometimes not be defined due to a > complex set of circumstances. > I would like to be able to find out whether or not it exists, and then > branch appropriately in my code. > > I had hoped to be able to use is.null() or exists() but neither of these > returns TRUE or FALSE which is the behaviour I would like, so that I can > write some appropriate branching code. > > is.null(meanedf) > Error: object 'meanedf' not found > No suitable frames for recover() > > exists(meanedf) > Error in exists(meanedf) : object 'meanedf' not found > > I would like some code which returns TRUE when meanedf exists and FALSE > when it doesn't. > > Thanks, and regards, > Margaret Donald > > > __ 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] Removing rows in a data frame
Try y - x[ -( 30596:678013 ), ] Please note that I have replaced 30595 with 30596 which is I think what you mean. You can add a new column with y$new - new_column # this is your vector of length 30595 Good luck, Rainer On Friday 03 July 2015 07:23:28 Charles Thuo wrote: I have a data frame whose rows are 678013 . I would like to remove rows from 30696 to 678013 and then attach a new column with a length of 30595. I tried Y- X[-30595:678013,] and its not working In addition how do i add a new column Kindly assist. Charles [[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.
Re: [R] Correlation matrix for pearson correlation (r,p,BH(FDR))
The way the sample data is provided is not useful. I have re-built your data, please find the dput() version below (and pls check whether I got it right...). This is not my area of competence at all, but from what I see from the help page is that the expected parameters are, among others: x A matrix or dataframe y A second matrix or dataframe __with the same number of rows as x__ I hope that somebody with a better understanding of your intention is able to pick up from here, with the sample data in useful format. Rgds, Rainer dput( genes ) structure(list(Genes = structure(1:10, .Label = c(KCNAB3, KCNB1, KCNB2, KERA, KGFLP1, KGFLP2, KHDC1, KHDC1L, KHDC3L, KHDRBS1), class = factor), Cell.line1 = c(12.02005181, 0.02457449, 0.44791862, 0.06090217, 0.02450101, 0, 0, 2.3189445, 0, 0), Cell.line2 = c(11.140091, 1.3028535, 0.1060137, 0, 0, 0, 0, 2.8252262, 0, 0), Cell.line3 = c(15.60381163, 0.81538294, 0.09864136, 0.03352993, 0, 0, 0, 5.29099724, 0, 0 ), Cell.line4 = c(13.44151596, 0.59318327, 0, 0.03634781, 0, 0, 0, 7.44183228, 0, 0), Cell.line5 = c(25.3716103, 0.15332321, 0, 0.04190912, 0, 0, 0, 1.94629741, 0, 0), Cell.line6 = c(8.12373424, 4.18181234, 0.05857207, 0, 0.02563099, 0, 0, 8.56022436, 0, 0 ), Cell.line7 = c(7.67506261, 1.65268403, 0.05945414, 0, 0.03902548, 0, 0, 7.50838343, 0, 0.0308118), Cell.line8 = c(24.43776341, 5.9834632, 0.20733924, 0.07752608, 0, 0, 0, 7.17964645, 0, 0), Cell.line9 = c(18.33244818, 1.51423807, 0.05830982, 0.01585643, 0, 0, 0, 3.28602729, 0, 0), Cell.line10 = c(9.224225, 0, 0, 16.664245, 0, 0, 0, 0, 3.598534, 2.600173)), .Names = c(Genes, Cell.line1, Cell.line2, Cell.line3, Cell.line4, Cell.line5, Cell.line6, Cell.line7, Cell.line8, Cell.line9, Cell.line10 ), row.names = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), class = data.frame) dput( features ) structure(list(Cell.line = c(Growth rate, Drug sensitivity ), Cell.line1 = c(NA, 41.33), Cell.line2 = c(NA, 26.76), Cell.line3 = c(NA, 24.19), Cell.line4 = c(51.41, NA), Cell.line5 = c(NA_character_, NA_character_), Cell.line6 = c(5.03, 1.40), Cell.line7 = c(6.57, 1.88), Cell.line8 = c(8, 1.33), Cell.line9 = c(1.26, 5.05), Cell.line10 = c(3, 9.12)), .Names = c(Cell.line, Cell.line1, Cell.line2, Cell.line3, Cell.line4, Cell.line5, Cell.line6, Cell.line7, Cell.line8, Cell.line9, Cell.line10), row.names = c(NA, -2L), class = data.frame) On Thu June 18 2015 10:19:55 Sarah Bazzocco wrote: This post was called help before, I changed the Subject. Thanks for the comments. Here the example: (I have the two lists saved as .csv and I can open them in R) Sheet one- Genes (10 genes expression, not binary, meaured in 10 cell lines) genes Genes Cell.line1 Cell.line2 Cell.line3 Cell.line4 Cell.line5 1 KCNAB3 12.02005181 11.1400910 15.60381163 13.44151596 25.37161030 2KCNB1 0.02457449 1.3028535 0.81538294 0.59318327 0.15332321 3KCNB2 0.44791862 0.1060137 0.09864136 0. 0. 4 KERA 0.06090217 0.000 0.03352993 0.03634781 0.04190912 5 KGFLP1 0.02450101 0.000 0. 0. 0. 6 KGFLP2 0. 0.000 0. 0. 0. 7KHDC1 0. 0.000 0. 0. 0. 8 KHDC1L 2.31894450 2.8252262 5.29099724 7.44183228 1.94629741 9 KHDC3L 0. 0.000 0. 0. 0. 10 KHDRBS1 0. 0.000 0. 0. 0. Cell.line6 Cell.line7 Cell.line8 Cell.line9 Cell.line10 1 8.12373424 7.67506261 24.43776341 18.332448189.224225 2 4.18181234 1.65268403 5.98346320 1.514238070.00 3 0.05857207 0.05945414 0.20733924 0.058309820.00 4 0. 0. 0.07752608 0.01585643 16.664245 5 0.02563099 0.03902548 0. 0.0.00 6 0. 0. 0. 0.0.00 7 0. 0. 0. 0.0.00 8 8.56022436 7.50838343 7.17964645 3.286027290.00 9 0. 0. 0. 0.3.598534 10 0. 0.03081180 0. 0.2.600173 Sheet two - features (2 features(Growth rate,drug sensitivity for 10 cell lines) features Cell.line Cell.line1 Cell.line2 Cell.line3 Cell.line4 Cell.line5 1 Growth rate NA NA NA 51.41 NA 2 Drug sensitivity 5.03 6.57 8 1.26 3 Cell.line6 Cell.line7 Cell.line8 Cell.line9 Cell.line10 1 41.33 26.76 24.19 NA NA 2 1.40 1.88 1.33 5.059.12 What I found: corr.test {psych} corr.test(x, y = NULL, use = pairwise,method=pearson,adjust=BH,alpha=.01) -- I adjusted the original command to what I need (BH insted og holm) and alpha=.01 insted of 0.05. I would be very happy, if someone could show me how to use this command, in particular
Re: [R] A simple For-Loop doesn't work
Hi Nick, Your code is not exactly commented, minimal, self-contained, reproducible and contains a number of inconsistencies (Data has three elements, your loop expects six). Try something like Data - c(July, August, September) ... for( x in Data ) { currentData - read.csv( paste( x, .csv, sep = ), header = TRUE ) ... } to get your loop going. Rgds, Rainer On Saturday 14 March 2015 18:28:28 Nicolae Doban wrote: Hello, my name is Nick and I'm working on a project. I'm having trouble with building a simple for-loop. In this loop I want to read csv files, perform a corr function and save it to a pdf file. I tried to solve this problem by looking for solutions online but couldn't figure it out. Could you also tell me if if it is possible to name the dataframe(grid.table())?Could you please help me? The code I wrote and which doesn't work is: *Code* Data - c(July, August, September) pdf(Cor.pdf) setwd(path) for(i in 1:6){ Data[i] - read.csv(Data[i],.csv, header=T) grid.table(cor(Data[i][3:10])) corrgram(Data[i], order=TRUE, lower.panel=panel.shade, upper.panel=panel.pie, text.panel=panel.txt, main=Data[i],Cor) } dev.off() */Code* Thank you, Nick [[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.
Re: [R] Installing gWidgetsRGtk2: R session is headless
Michael, thanks, that was it! I had installed the packages as root and tried them out as root (not a good idea I know, but I was lazy), while running the the X11 display as user. Worse is that I have done that many times. One more thing learned. Thanks again, Rainer On Thursday 23 October 2014 19:35:00 Michael Lawrence wrote: Perhaps this is a permissions (Xauthority) issue: is the same user running both the X11 display and the R session? On Thu, Oct 23, 2014 at 2:40 AM, R rainer.schuerm...@gmx.net wrote: I have written some gWidgets scripts before in the past but have a different box now (Debian KWheezy) and cannot get gWidgets working. It may be an obvious mistake but auntie Google (who has helped me a lot to get as far as I am now) leaves me in the dark now. Here is where I am stuck: - - - - - library( gWidgets ) library( gWidgetsRGtk2 ) Loading required package: RGtk2 No protocol specified R session is headless; GTK+ not initialized. obj - gbutton(Hello world, container = gwindow()) (R:15675): GLib-GObject-WARNING **: invalid (NULL) pointer instance (R:15675): GLib-GObject-CRITICAL **: g_signal_connect_data: assertion `G_TYPE_CHECK_INSTANCE (instance)' failed (R:15675): Gtk-WARNING **: Screen for GtkWindow not set; you must always set a screen for a GtkWindow before using the window (R:15675): Gdk-CRITICAL **: IA__gdk_screen_get_default_colormap: assertion `GDK_IS_SCREEN (screen)' failed (R:15675): Gdk-CRITICAL **: IA__gdk_colormap_get_visual: assertion `GDK_IS_COLORMAP (colormap)' failed (R:15675): Gdk-CRITICAL **: IA__gdk_screen_get_default_colormap: assertion `GDK_IS_SCREEN (screen)' failed (R:15675): Gdk-CRITICAL **: IA__gdk_screen_get_root_window: assertion `GDK_IS_SCREEN (screen)' failed (R:15675): Gdk-CRITICAL **: IA__gdk_screen_get_root_window: assertion `GDK_IS_SCREEN (screen)' failed (R:15675): Gdk-CRITICAL **: IA__gdk_window_new: assertion `GDK_IS_WINDOW (parent)' failed *** caught segfault *** address 0x18, cause 'memory not mapped' Traceback: 1: .Call(name, ..., PACKAGE = PACKAGE) 2: .RGtkCall(S_gtk_widget_show, object, PACKAGE = RGtk2) 3: method(obj, ...) 4: window$Show() 5: .gwindow(toolkit, title, visible, width, height, parent, handler, action, ...) 6: .gwindow(toolkit, title, visible, width, height, parent, handler, action, ...) 7: gwindow() 8: .gbutton(toolkit, text, border, handler, action, container, ...) 9: .gbutton(toolkit, text, border, handler, action, container, ...) 10: gbutton(Hello world, container = gwindow()) - - - - - sessionInfo() R version 3.1.1 (2014-07-10) Platform: x86_64-pc-linux-gnu (64-bit) locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C [3] LC_TIME=en_US.UTF-8LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=en_US.UTF-8LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=en_US.UTF-8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base loaded via a namespace (and not attached): [1] tools_3.1.1 __ 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-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] format negative numbers
Maybe not the most elegant way but at least works: library( stringr ) x - as.factor( 123.4- ) x - -as.numeric( str_replace( as.character( x ), -, ) ) x [1] -123.4 On Monday 20 October 2014 09:03:36 PIKAL Petr wrote: Dear all. Before I start fishing in (for me) murky regular expression waters I try to ask community about changing format of negative numbers. For some reason I get a file with negative numbers formatted with negative sign at end of number. something like 0.123- It is imported as factors and I need to convert it to numbers again. Before converting I need to change it to correct format -0.123 Does anybody know some simple way? Cheers Petr . __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ 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] Which() missing a number
... and it is also missing the 15 at position 15. Can't explain but which( neilist %in% pfriends ) should give you what you want. On Tuesday 18 March 2014 11:14:08 Thomas wrote: Does anyone know why this is happening? Which() is picking up the indices of the numbers 13 and 15 in neilist, but it's missing out the 13 at index 6. Thank you, Thomas Chesney neilist [1] 13 15 28 29 30 13 14 15 28 30 43 44 45 14 15 29 44 45 pfriends [1] 13 15 which(neilist==pfriends) [1] 1 2 8 neilist[6] [1] 13 str(neilist) int [1:18] 13 15 28 29 30 13 14 15 28 30 ... str(pfriends) num [1:2] 13 15 sessionInfo() R version 2.15.3 (2013-03-01) Platform: i386-apple-darwin9.8.0/i386 (32-bit) locale: [1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8 attached base packages: [1] splines grid stats graphics grDevices utils datasets methods [9] base other attached packages: [1] spdep_0.5-56coda_0.16-1 deldir_0.0-21 maptools_0.8-23 foreign_0.8-52 [6] nlme_3.1-108MASS_7.3-23 Matrix_1.0-11 lattice_0.20-13 boot_1.3-7 [11] sp_1.0-8igraph_0.6.5-1 loaded via a namespace (and not attached): [1] LearnBayes_2.12 tools_2.15.3 This message and any attachment are intended solely for the addressee and may contain confidential information. If you have received this message in error, please send it back to me, and immediately delete it. Please do not use, copy or disclose the information contained in this message or in any attachment. Any views or opinions expressed by the author of this email do not necessarily reflect the views of the University of Nottingham. This message has been checked for viruses but the contents of an attachment may still contain software viruses which could damage your computer system, you are advised to perform your own checks. Email communications with the University of Nottingham may be monitored as permitted by UK legislation. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] question on more efficient data block-match processing
What I would do: # read in your sample data mbr - read.table( clipboard, header = TRUE, stringsAsFactors = FALSE ) # create a vector with the codes you want to consider code.list - c(A,B,C,D,E) # reduce the data accordingly mbr - mbr[ mbr$code %in% code.list, ] # get your model matrix using reshape library( reshape ) model.matrix - as.data.frame( cast( melt( mbr ), value ~ code ) ) # Cosmetics colnames( model.matrix )[1] - Member model.matrix[ 2 : ( length( model.matrix[1,] ) ) ] - ifelse( model.matrix[ 2 : ( length( model.matrix[1,] ) ) ] 0, 1, 0 ) On Thursday 06 March 2014 19:23:03 Mckinstry, Craig wrote: I have a medical insurance claims datafile divided into blocks by member, with multiple lines per member. I am process these into a one line per member model matrix. Member block sizes vary from 1 to 50+. I am match attributes in claims data to columns in the model matrix and have been getting by with a for loop, but for large file size it takes much too long. Is there vectorized/apply based method to do this more efficiently? input data: membercode 1 A 1 C 1 F 2 B 2 E 3 D 3 A 3 B 3 D 4 G 4 A code.list - c(A,B,C,D,E) for(i in 1:n.mbr){ mbr.i - dat[dat$Rmbr==mbr.list[i],]#EXTRACT BLOCK OF MEMBER CLAIMS matrix.mat[i,unique(match(mbr.i$code,code.list))] - 1 } output model.matrix MemberA B C D E 1 1 0 1 0 0 2 0 1 0 0 1 3 1 1 0 1 0 4 1 0 0 0 0 Craig McKinstry 100 Market, 6th floor Office: 503-225-6878 | Cell: 509-778-2438 IMPORTANT NOTICE: This communication, including any attachment, contains information that may be confidential or privileged, and is intended solely for the entity or individual to whom it is addressed. If you are not the intended recipient, you should delete this message and are hereby notified that any disclosure, copying, or distribution of this message is strictly prohibited. Nothing in this email, including any attachment, is intended to be a legally binding signature. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] keep track of variables created in each chapter of a knitr book
You could initialize one list per chapter, x1 - list( Chapter One ) and then crate your variables as list members x1$A - c( 1, 2, 3 ) x1$B - bla x1$tv.data - data.frame( m = sample( LETTERS, 5 ), n = round( runif( 5 ), 2 ) ) x1 [[1]] [1] Chapter One $A [1] 1 2 3 $B [1] bla $tv.data nm 1 T 0.92 2 G 0.77 3 B 0.96 4 W 0.67 5 S 0.16 which you can keep track of easily at any time. You could save each list per chapter, if you wanted to. And you can remove it as easily with a simple rm (x1 ) A bit more typing, but much cleaner, I think. Rgds, Rainer On Friday 24 January 2014 09:14:39 Michael Friendly wrote: In a book project using knitr, I'm creating a large number of variable and objects in chunks within chapters. I'd like to find a way of keeping track of all of those for each chapter, and clean up at the end of each chapter, without having to manually list their names as shown below. The book.Rnw file uses a collection of child documents: ch1, child='ch01.Rnw'= @ ch2, child='ch02.Rnw'= @ ch3, child='ch03.Rnw'= @ ... A typical chapter file, ch02.Rnw begins with a setup chunk and ends with a cleanup chunk: setup2, echo=FALSE= source(Rprofile.R) knitrSet(ch02) require(vcdExtra, quietly = TRUE, warn.conflicts = FALSE) @ content ... cleanup2,results='hide'= remove(list=objects(pattern=array|mat|my|\\.tab|\\.df)) remove(list=c(A, B, age, count, ds, n, passed, sex, tab, tv.data, TV2, TV)) ls() @ __ 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] Generate Variable Length Strings from Various Sources
### How I would do it: # container for the result res - NULL # number of strings to be created n - 50 # random length of each string v.length = sample( c( 2:4), n, rep = TRUE ) # letter sources src.1 = LETTERS[ 1:10 ] src.2 = LETTERS[ 11:20 ] src.3 = z src.4 = c( 1, 2 ) # turn into a list src - list( src.1, src.2, src.3, src.4 ) # use a loop for( i in 1:n ) { res[[i]] - paste( sample( src[[ sample( 1:4, 1 ) ]], v.length[ i ], rep = TRUE ), collapse = ) } res - unlist( res ) [1] RLOK 22 CCA IEC zz 111 12 zzz KOS 12 [11] 2212 212 HFG zzz 11 TRM FGBA zz LLLR [21] 211 21 SSKR BEDD NK LO 221 GDE MNOT zz [31] DHD RMSS PSO 111 zz EFFF JAB BBB QQRN [41] FDG zz CDE 111 zz zzz GAB zzz JGGD On Wednesday 15 January 2014 20:15:03 Burhan ul haq wrote: # Function to generate a string, given: # its length(passed as len) # and the source(passed as src) my.f = function(len,src) { tmp = sample(src,len,rep=FALSE) n1 = paste(tmp,collapse=) n1 } # end # count n=50 # length of names, a variable indicating string length v.length = sample(c(2,3,4),n,rep=TRUE) # letter sources src.1 = LETTERS[1:10] src.2 = LETTERS[11:20] src.3 = z src.4 = c(1,2) # Issue #s.ind = sample(c(src.1,src.2),n,rep=TRUE) s.ind = sample(c(src.1,src.3,src.4),n,rep=TRUE) # Generate n strings, whose length is given by v.length, and randomly using sources (src1 to 4) unlist(lapply(v.length,my.f,s.ind)) __ 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] xtable: custom row.names, move caption to top
You get the cation to the top of the table with print( saxtab, caption.placement = top ) Formatting the table the way you want can be done like this - I did not manage to carry the LaTeX math formatting for the row names over ($k$ and $n_k$)but the rest should be very much what you want: saxtab - t( as.data.frame( addmargins( Saxony ) ) ) rownames( saxtab ) - c( Males (k), Families (n_k) ) saxtab - xtable( saxtab, digits = 0, caption = Number of male children in 6115 Saxony families of size 12, align = l|rr ) print( saxtab, caption.placement = top, include.colnames = FALSE, hline.after = c( NULL, 0, nrow( saxtab ) ) ) % latex table generated in R 3.0.2 by xtable 1.7-1 package % Wed Nov 27 17:41:17 2013 \begin{table}[ht] \centering \caption{Number of male children in 6115 Saxony families of size 12} \begin{tabular}{l|rr} \hline Males (k) 0 1 2 3 4 5 6 7 8 9 10 11 12 Sum \\ Families (n\_k) 324 104 286 670 1033 1343 1112 829 478 18145 7 6115 \\ \hline \end{tabular} \end{table} On Wednesday 27 November 2013 09:24:22 Michael Friendly wrote: With xtable, I'm producing one-way tables from table objects in horizontal form as shown below. I'd like to change the labels used for the rows and move the caption to the top of the table, as is typically standard for tables. I can hand-edit, but would prefer to do it in code. data(Saxony, package=vcd) library(xtable) saxtab - xtable(t(addmargins(Saxony)), digits=0, caption=Number of male children in 6115 Saxony families of size 12) print(saxtab) print(saxtab) % latex table generated in R 3.0.1 by xtable 1.7-1 package % Wed Nov 27 09:12:16 2013 \begin{table}[ht] \centering \begin{tabular}{rrr} \hline 0 1 2 3 4 5 6 7 8 9 10 11 12 Sum \\ \hline 1 3 24 104 286 670 1033 1343 1112 829 478 181 45 7 6115 \\ \hline \end{tabular} \caption{Number of male children in 6115 Saxony families of size 12} \end{table} The desired form looks like this, with row.names = c(Males ($k$), Families ($n_k$)) % latex table generated in R 3.0.1 by xtable 1.7-1 package % Tue Nov 26 14:56:02 2013 \begin{table}[ht] \caption{Number of male children in 6115 Saxony families of size 12} \label{tab:saxtab} \centering \begin{tabular}{l|rr} \hline Males ($k$) 0 1 2 3 4 5 6 7 8 9 10 11 12 Sum \\ \hline Families ($n_k$) 3 24 104 286 670 1033 1343 1112 829 478 181 45 7 6115 \\ \hline \end{tabular} \end{table} __ 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] xtable: custom row.names, move caption to top
UPDATE: Now including the LaTeX math formatting saxtab - t( as.data.frame( addmargins( Saxony ) ) ) rownames( saxtab ) - c( Males ($k$), Families ($n_k$) ) saxtab - xtable( saxtab, digits = 0, caption = Number of male children in 6115 Saxony families of size 12, align = l|rr ) print( saxtab, caption.placement = top, include.colnames = FALSE, hline.after = c( NULL, 0, nrow( saxtab ) ), sanitize.text.function = function(x) { x } ) % latex table generated in R 3.0.2 by xtable 1.7-1 package % Wed Nov 27 18:16:47 2013 \begin{table}[ht] \centering \caption{Number of male children in 6115 Saxony families of size 12} \begin{tabular}{l|rr} \hline Males ($k$) 0 1 2 3 4 5 6 7 8 9 10 11 12 Sum \\ Families ($n_k$) 324 104 286 670 1033 1343 1112 829 478 18145 7 6115 \\ \hline \end{tabular} \end{table} On Wednesday 27 November 2013 17:43:30 Rainer Schuermann wrote: You get the cation to the top of the table with print( saxtab, caption.placement = top ) Formatting the table the way you want can be done like this - I did not manage to carry the LaTeX math formatting for the row names over ($k$ and $n_k$)but the rest should be very much what you want: saxtab - t( as.data.frame( addmargins( Saxony ) ) ) rownames( saxtab ) - c( Males (k), Families (n_k) ) saxtab - xtable( saxtab, digits = 0, caption = Number of male children in 6115 Saxony families of size 12, align = l|rr ) print( saxtab, caption.placement = top, include.colnames = FALSE, hline.after = c( NULL, 0, nrow( saxtab ) ) ) % latex table generated in R 3.0.2 by xtable 1.7-1 package % Wed Nov 27 17:41:17 2013 \begin{table}[ht] \centering \caption{Number of male children in 6115 Saxony families of size 12} \begin{tabular}{l|rr} \hline Males (k) 0 1 2 3 4 5 6 7 8 9 10 11 12 Sum \\ Families (n\_k) 324 104 286 670 1033 1343 1112 829 478 18145 7 6115 \\ \hline \end{tabular} \end{table} On Wednesday 27 November 2013 09:24:22 Michael Friendly wrote: With xtable, I'm producing one-way tables from table objects in horizontal form as shown below. I'd like to change the labels used for the rows and move the caption to the top of the table, as is typically standard for tables. I can hand-edit, but would prefer to do it in code. data(Saxony, package=vcd) library(xtable) saxtab - xtable(t(addmargins(Saxony)), digits=0, caption=Number of male children in 6115 Saxony families of size 12) print(saxtab) print(saxtab) % latex table generated in R 3.0.1 by xtable 1.7-1 package % Wed Nov 27 09:12:16 2013 \begin{table}[ht] \centering \begin{tabular}{rrr} \hline 0 1 2 3 4 5 6 7 8 9 10 11 12 Sum \\ \hline 1 3 24 104 286 670 1033 1343 1112 829 478 181 45 7 6115 \\ \hline \end{tabular} \caption{Number of male children in 6115 Saxony families of size 12} \end{table} The desired form looks like this, with row.names = c(Males ($k$), Families ($n_k$)) % latex table generated in R 3.0.1 by xtable 1.7-1 package % Tue Nov 26 14:56:02 2013 \begin{table}[ht] \caption{Number of male children in 6115 Saxony families of size 12} \label{tab:saxtab} \centering \begin{tabular}{l|rr} \hline Males ($k$) 0 1 2 3 4 5 6 7 8 9 10 11 12 Sum \\ \hline Families ($n_k$) 3 24 104 286 670 1033 1343 1112 829 478 181 45 7 6115 \\ \hline \end{tabular} \end{table} __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Installing RCurl: 'configure' exists but is not executable
I'm trying to install.packages( RCurl ) as root but get ERROR: 'configure' exists but is not executable I remember having had something like that before on another machine and tried in bash what is described here http://mazamascience.com/WorkingWithData/?p=1185 and helped me before: # mkdir ~/tmp # export TMPDIR=~/tmp and added, just in case, # chmod u+x $TMPDIR which apparently does what it should # ls -ld $TMPDIR drwxrwxrwx 2 root root 4096 Nov 1 08:59 /root/tmp but it doesn't help, I get the same error. What else can I try? Thanks in advance, Rainer sessionInfo() R version 3.0.2 (2013-09-25) Platform: x86_64-pc-linux-gnu (64-bit) locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C [3] LC_TIME=en_US.UTF-8LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=en_US.UTF-8LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=en_US.UTF-8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base loaded via a namespace (and not attached): [1] tools_3.0.2 __ 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] Installing RCurl: 'configure' exists but is not executable
# ls -l `which curl-config` -rwxr-xr-x 1 root root 6327 Oct 20 15:25 /usr/bin/curl-config On Friday 01 November 2013 20:21:36 Michael Hannon wrote: The error message doesn't seem to refer to the tmp directory. What do you get from: ls -l `which curl-config` -- Mike On Fri, Nov 1, 2013 at 7:43 PM, Rainer Schuermann rainer.schuerm...@gmx.net wrote: I'm trying to install.packages( RCurl ) as root but get ERROR: 'configure' exists but is not executable I remember having had something like that before on another machine and tried in bash what is described here http://mazamascience.com/WorkingWithData/?p=1185 and helped me before: # mkdir ~/tmp # export TMPDIR=~/tmp and added, just in case, # chmod u+x $TMPDIR which apparently does what it should # ls -ld $TMPDIR drwxrwxrwx 2 root root 4096 Nov 1 08:59 /root/tmp but it doesn't help, I get the same error. What else can I try? Thanks in advance, Rainer sessionInfo() R version 3.0.2 (2013-09-25) Platform: x86_64-pc-linux-gnu (64-bit) locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C [3] LC_TIME=en_US.UTF-8LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=en_US.UTF-8LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=en_US.UTF-8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base loaded via a namespace (and not attached): [1] tools_3.0.2 __ 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-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] First time r user
It would be helpful if - you give us some sample data: dput( head( myData ) ) - tell us what kind of function you want to apply, or how the result looks like that you want to achieve - show us what you have done so far, and where you are stuck On Saturday 17 August 2013 19:33:08 Dylan Doyle wrote: Hello R users, I have recently begun a project to analyze a large data set of approximately 1.5 million rows it also has 9 columns. My objective consists of locating particular subsets within this data ie. take all rows with the same column 9 and perform a function on that subset. It was suggested to me that i use the ddply() function from the Pylr package. Any advice would be greatly appreciated Thanks much, Dylan [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to vectorize subsetting
I'm sure there are better, more elegant ways avoiding the nested loop I'm suggesting - but if it was my problem, here is what I would do (assuming that my understanding of your question is correct): ### separate function for 'doing something' with the data subset do.something - function( qA, qB ) { # printing the response subsets as a substitute for your do something print( qA ) print( qB ) # you could use a list here to organise or return your results } ### vector containing the numbers of the areas areas - unique( yrData$Area ) ### extract question headers questions - colnames( yrData )[ !colnames( yrData ) %in% c( Area, Facility ) ] ### loop through your Area for( i in areas ) { # subset per Area yrSubData - yrData[ yrData$Area == i, ] # vector containing the Facilities in that Area facilities - yrSubData$Facility # loop through your Facilities for( j in facilities ) { # get subsets A - yrSubData[ yrSubData$Facility != j, ] B - yrSubData # for each combination of subsets, loop through the questions for( q in questions ) { do.something( A[ q ], B[ q ] ) } } } Output: Q1 2 3 3 1 Q1 1 2 2 3 3 1 Q1 1 2 3 1 Q1 1 2 2 3 3 1 Q1 1 2 2 3 Q1 1 2 2 3 3 1 Q1 5 5 6 2 Q1 4 4 5 5 6 2 Q1 4 4 6 2 Q1 4 4 5 5 6 2 Q1 4 4 5 5 Q1 4 4 5 5 6 2 which is what I think should satisfy your need as a first step. Rgds, Rainer On Wednesday 14 August 2013 07:20:24 Derickson, Ryan, VHACIN wrote: Hello all, I've tried to solve this for weeks and posted to other forums with little success- I'd appreciate any help from anyone. I have survey data grouped by facility and area (area is a collection of facilities). Questions are q1-q10. For each facility, I need to subset each item into the facility's responses, and the facility's area responses excluding the facility. This might illustrate it better: Area FacilityQ1... Q10 1 1 2 1 2 3 1 3 1 2 4 4 2 5 5 2 6 2 A- Select Q1 for all Area=1 and Facility!=1; B- Select Q1 for all Facility=1; do something with A and B A- Select Q1 for all Area=1 and Facility!=2; B- Select Q1 for all Facility=2; do something with A and B A- Select Q1 for all Area=1 and Facility!=3; B- Select Q1 for all Facility=3; do something with A and B ... A- Select Q10 for all Area=2 and Facility!=6; B- Select Q10 for all Facility=6; do something with A and B I know how to write the code to manually pull each subset, but I have a lot of facilities and areas that get renamed from year to year so I need to vectorize my code so each subset doesn't have to be explicitly called by area or facility name. Again, I would be incredibly appreciative of any help. I'm at a dead-end. Ryan __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Week number for a given date
What about ?lubridate particulatrly week()? On Wednesday 14 August 2013 22:00:42 Christofer Bogaso wrote: Hello again, I need to calculate the week number of the corresponding month given a date. Is there any function available with R to calculate that? Thanks and regards, [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Using text and variable in ggtitle (ggplot2)
Not sure whether I understand your question fully but I guess paste() is your friend: ggtitle( paste( RR(overall) =, RR, N =, N, alpha =, alpha1 ) ) On Friday 19 July 2013 17:17:24 Manisha Brahma chary wrote: Hello, I am using ggplot2 to plot a graph and I want to give a title to the plot that has both text and variables. I have tried a couple of ways, but none of the methods work. Below is my code. Under ggtitle RR, N, alpha1 are variables and RR(overall), N, alpha are strings. Require(ggplot2) data - do.call(rbind,result) data.new - as.data.frame(cbind(data$p1,(data$prob),data$r)) colnames(data.new) - c(ES,Probability,Vector) bp-(ggplot(data.new,aes(x=ES,y=Probability,group=Vector,colour=factor(Vector))) + geom_line()) bp.title- bp+ ggtitle(RR(overall) =RR, N = N, alpha = alpha1) bp.title Any suggestion on ggplot2 will be much appreciated. Thanks Man [[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. - - - - - Der NSA keine Chance: e-mail verschluesseln! http://www.gpg4win.org/ __ 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] Serialize data.frame to database
Maybe a simple dbWriteTable( db, frames, iris ) does what you want? On Monday 15 July 2013 23:43:18 Simon Zehnder wrote: Dear R-Users, I need a very fast and reliable database solution so I try to serialize a data.frame (to binary data) and to store this data to an SQLite database. This is what I tried to do: library(RSQLite) con - dbDriver(SQLite) db - dbConnect(con, test) dbSendQuery(db, 'CREATE TABLE frames(simID INT, data BLOB)') data.bin - serialize(iris, NULL, ascii = FALSE) dbSendQuery(db, paste(INSERT INTO frames VALUES(1, X', data.bin, '), sep = )) data.bin2 - dbGetQuery(db, SELECT DATA FROM frames WHERE simID = 1) data.bin2 data 1 58 So, only the first entry of data.bin is saved to the database. I tried to first convert the binary data to raw data: data.raw - rawToChar(data.bin) Error in rawToChar(data.bin) : embedded nul in string: 'X\n\0\0\0\002\0\003\0\001\0\002\003\0\0\0\003\023\0\0\0\005\0\0\0\016\0\0\0\x96@\024ff@\023\x99\x99\x99\x99\x99\x9a@\022\xcc\xcc\xcc\xcc\xcc\xcd@\022ff@\024\0\0\0\0\0\0@\025\x99\x99\x99\x99\x99\x9a@\022ff@\024\0\0\0\0\0\0@\021\x99\x99\x99\x99\x99\x9a@\023\x99\x99\x99\x99\x99\x9a@\025\x99\x99\x99\x99\x99\x9a@\02333@\02333@\02133@\02733@\026\xcc\xcc\xcc\xcc\xcc\xcd@\025\x99\x99\x99\x99\x99\x9a@\024ff@\026\xcc\xcc\xcc\xcc\xcc\xcd@\024ff@\025\x99\x99\x99\x99\x99\x9a@\024ff@\022ff@\024ff@\02333@\024\0\0\0\0\0\0@\024\0\0\0\0\0\0@\024\xcc\xcc\xcc\xcc\xcc\xcd@\024\xcc\xcc\xcc\xcc\xcc\xcd@\022\xcc\xcc\xcc\xcc\xcc\xcd@\02333@\025\x99\x99\x99\x99\x99\x9a@\024\xcc\xcc\xcc\xcc\xcc\xcd@\026\0\0\0\0\0\0@\023\x99\x99\x99\x99\x99\x9a@\024\0\0\0\0\0\0@\026\0\0\0\0\0\0@\023\x99\x99\x99\x99\x99\x9a@\021\x99\x99\x99\x99\x99\x9a@\024ff@\024\0\0\0\0\0\0@\022\0\0\0\0\0\0@\021\x99\x99\x99\x99\x99\x9a@\024\0\0\0\! 0\! 0\0 I don't know what this error should tell me. Then I tried to use the ASCII format data.ascii - serialize(iris, NULL, ascii = TRUE) data.raw - rawToChar(data.ascii) dbSendQuery(db, DELETE FROM frames) dbSendQuery(db, paste(INSERT INTO frames VALUES(1, X', data.raw, '), sep = )) Error in sqliteExecStatement(conn, statement, ...) : RS-DBI driver: (error in statement: unrecognized token: X'A This also does not work. It seems the driver does not deal that nicely with the regular INSERT query for BLOB objects in SQLite. Then I used a simpler way: dbSendQuery(db, DELETE FROM frames) dbSendQuery(db, DROP TABLE frames) dbSendQuery(db, 'CREATE TABLE frames(simID INT, data TEXT DEFAULT NULL)') dbSendQuery(db, paste(INSERT INTO frames VALUES(1, ', data.raw, '), sep = )) data.bin2 - dbGetQuery(db, SELECT data FROM frames WHERE simID = 1) Nice, that worked. Now I want to unserialize the data: unserialize(data.bin2) Error in unserialize(data.bin2) : 'connection' must be a connection unserialize(data.bin2[1, 'data']) Error in unserialize(data.bin2[1, data]) : character vectors are no longer accepted by unserialize() I feel a little stuck here, but I am very sure, that converting data.frames to binary data and storing them to a database is not that unusual. So I hope somebody has already done this and could give me the missing piece. Best Simon __ 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. - - - - - Der NSA keine Chance: e-mail verschluesseln! http://www.gpg4win.org/ __ 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] Fetch and merge from a data set
Is merge( data[1], data1 ) what you want? On Tuesday 25 June 2013 12:00:23 Nico Met wrote: Many thanks Rui, However If I want to extract only first column (data1) from data file, then how Can I do it? Thanks again Nico On Tue, Jun 25, 2013 at 11:43 AM, Rui Barradas ruipbarra...@sapo.pt wrote: Hello, I'm not sure I understand, but it seems as simple as merge(data1, data) Hope this helps, Rui Barradas Em 25-06-2013 10:34, Nico Met escreveu: Dear all, I would like to fetch a list (data1) of entities from a big data file (data) and merged together. for example: data is the file from where I want to extract dput(data) structure(list(NAME = structure(c(6L, 6L, 7L, 6L, 6L, 7L, 6L, 7L, 3L, 5L, 3L, 3L, 3L, 3L, 4L, 3L, 3L, 4L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c(CDS0008_3, CDS0008_9, CDS0248_2, CDS0248_3, CDS0248_9, CDS0644, CDS0645), class = factor), QUAL = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c(CE, IE, IL, NL), class = factor), DIFF = c(-1.3998944145561, -3.2771509067793, -3281.0765147, 8.434493332, 1.825, -0.584379415213178, -0.842892819141902, -7939.444248, 0.305343667019102, 11.4859716384148, 1242.7216649, 33.25400, -14.8, 62.8783807080717, 0.3234355, 3325.4720195, 33.8787927988058, -26.772370001, 58.6, 58.6, 0.0566947723248923, 1.294147, -13.319016665, -18.2, -76.008156572, -13.319016665, -18.2, -13.319016665, -18.2, -2632.77274125, -42.6985227297727, -19.2272727272727, -640.535327886362), PVALUE = c(0.393708851005828, 0.213217899579307, 0.59042649939326, 0.874157227673879, 0.953482720079014, 0.737143165148719, 0.76195136190783, 0.27992628190224, 0.737143165148719, 0.76195136190783, 0.27992628190224, 0.672772547835936, 0.86918514824479, 0.86918514824479, 0.794677241773376, 0.67568899695407, 0.0792182671307693, 0.53713122011077, 0.298506678908869, 0.343822055403655, 0.962553162399683, 0.335590623453015, 0.962553162399683, 0.335590623453015, 0.966426100547593, 0.671619043425778, 0.225088848812347, 0.962553162399683, 0.335590623453015, 0.17524845367103, 0.205476355179601, 0.229212899348569, 0.739457737437997 ), MEAD = c(61.997380489015, 38.9012158419308, 42541.899878, 644.34279333, 58.8, 65.4391126224113, 45.1617170900398, 37470.374736, 61.6954795437718, 36.4397768557611, 26367.622967, 485.3921, 76, 0, 0.8717201, 28005.1589441667, 46.2203164422305, 713.02971667, 64, 64, 23.0947770774977, 1.456775, 79.2102758175251, 39.5498022382743, 31387.15404, 883.47568, 180, 76.8751996288758, 41.0701371024372, 23960.47775025, 746.3317500025, 104.5, 17488.00655825 )), .Names = c(NAME, QUAL, DIFF, PVALUE, MEAD), class = data.frame, row.names = c(NA, 33L)) And data1 : subset of entities dput(data1) structure(list(NAME = structure(c(5L, 4L, 2L, 3L, 1L), .Label = c(CDG981, CDS0248_2, CDS0248_9, CDS0644, CDS0645), class = factor), VAL = structure(c(1L, 2L, 5L, 3L, 4L), .Label = c(YU1, YU2, YU4, YU5, YU7), class = factor)), .Names = c(NAME, VAL), class = data.frame, row.names = c(NA, 5L)) Please help me how can I do it. Many thanks Nico [[alternative HTML version deleted]] __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html 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. - - - - - Der NSA keine Chance: e-mail verschluesseln! http://www.gpg4win.org/ __ 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] Creating subset using selected columns
Supposed your data.frame is called x, try x[ which( substr( colnames( x ), 1, 4 ) == Peak ) ] On Sunday 16 June 2013 15:20:37 Suparna Mitra wrote: Hello R experts, I need a help to create a subset file. I know with subset comand, its very easy to select many different columns, or threshold. But here I have a bit problem as in my data file is big. And I don't want to identify the column numbers or names manually. I am trying to find any way to automatise this. For example I have a file with about 1500 columns from TRFLP intensity data. And the column names are like: [1] Sample.NameMarker RE Dye Allele.1 Size.1 Height.1 Peak.Area.1 Data.Point.1 [10] Allele.2 Size.2 Height.2 Peak.Area.2 Data.Point.2 Allele.3 Size.3 Height.3 Peak.Area.3 [19] Data.Point.3 Allele.4 Size.4 Height.4 Peak.Area.4Data.Point.4 Allele.5 Size.5 Height.5 [28] Peak.Area.5Data.Point.5 Allele.6 Size.6 Height.6 Peak.Area.6Data.Point.6 Allele.7 Size.7 [37] Height.7 Peak.Area.7Data.Point.7 Allele.8 Size.8 Height.8 Peak.Area.8Data.Point.8 Allele.9 [46] Size.9 Height.9 Peak.Area.9Data.Point.9 Allele.10 Size.10Height.10 Peak.Area.10 Data.Point.10 . Suppose I want to create a subset selecting all the columns with name Peak.Area (as in unix Peak.Area.*) How can I do that in R? Thanks a lot for the help. Best wishes, Mitra [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] rename and concatenate name of columns
df1 - data.frame( A = runif( 10 ), B = runif( 10 ) * 5, C = runif( 10 ) * 10, D = runif( 10 ) * 20 ) df2 - data.frame( X = runif( 10 ), Y = runif( 10 ) * 5, Z = runif( 10 ) * 10 ) rename_columns - function( dataset ) { for( i in 2:ncol( dataset ) ) colnames( dataset )[i] - paste( colnames( dataset )[1], colnames( dataset )[i], sep = _ ) return( dataset ) } df1 - rename_columns( df1 ) df1 A A_B A_C A_D 1 0.79914742 4.4898911 2.8869670979 11.067921 2 0.34552180 3.5456202 7.4774973304 8.610913 3 0.02950957 3.1754457 2.4493258283 17.649965 4 0.41804920 3.6180818 2.4680344155 5.038590 5 0.69496877 1.7169792 0.8790624910 3.476689 6 0.79293910 0.4452258 9.5359219401 3.668066 7 0.06135734 0.9188141 8.4256720915 9.980292 8 0.86066946 1.2674735 4.1982888198 13.561160 9 0.41895427 0.1434889 0.0007853331 3.187101 10 0.09498189 4.5215823 1.0008131783 10.428556 On Friday 14 June 2013 18:04:22 Arman Eshaghi wrote: Dear all, I have different data frames for which I would like to modify names of each column such that the new name would include the name of the first column added to the name of other columns; I came up with the following code. Nothing changes when I run the following code. I would be grateful if someone could help me. All the best, -Arman rename_columns - function(dataset) { for (i in 2:(ncol(dataset))) {names(dataset)[i] - paste(names(dataset)[1], names(dataset)[i], sep=_) } } rename_columns(dataset) %nothing happens! [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] checking certain digits of a number in a column
Try ?grep or library( stringr ) ?str_detect On Saturday 15 June 2013 00:33:31 Yasin Gocgun wrote: Hi, I need to check whether certain digits of a number, say, last five digits, appear in a column of a data frame. For instance, For example,103 in 000103 ( data [data[,3] == ...103] instead of data [data[,3] == 000103] ) or 54780 in 12354780. Can someone let me know how to do so. Thanks in advance, Yasin [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] loops for matrices
The comments on StackOverflow are fair, I believe... Please dput() your matrices, so that your code becomes reproducible! On Wednesday 12 June 2013 11:14:35 maggy yan wrote: I have to use a loop (while or for) to return the result of hadamard product. now it returns a matrix, but when I use is.matrix() to check, it returns FALSE, whats wrong? Matrix.mul - function(A, B) { while(is.matrix(A) == FALSE | is.matrix(B) == FALSE ) {print(error) break} while(is.matrix(A) == T is.matrix(B) == T) { n - dim(A)[1]; m - dim(A)[2]; p - dim(B)[1]; q - dim(B)[2]; while(m == p) { C - matrix(0, nrow = n , ncol = q) for(s in 1:n) { for(t in 1:q) { c - array(0, dim = m ) for(k in 1:m) { c[k] - A[s,k] * B[k, t] } C[s, t] - sum(c) } } print(C) break } while(m != p) { print(error) break } break } } [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] error in rowSums in data.table
On Thursday 30 May 2013 02:57:04 Camilo Mora wrote: do you know why is this? or is there another way to sum by row in a given number of columns? Without data.table: x - structure(list(col1 = c(NA, 0, -0.015038, 0.003817, -0.011407 ), col2 = c(0.003745, 0.007463, -0.007407, -0.003731, -0.007491 )), .Names = c(col1, col2), class = data.frame, row.names = c(NA, -5L)) x[ is.na( x ) ] - 0 x$col3 - x$col1 + x$col2 x col1 col2 col3 1 0.00 0.003745 0.003745 2 0.00 0.007463 0.007463 3 -0.015038 -0.007407 -0.022445 4 0.003817 -0.003731 0.86 5 -0.011407 -0.007491 -0.018898 #col1 col2 col3 #1:NA 0.003745 0.003745 #2: 0.00 0.007463 0.007463 #3: -0.015038 -0.007407 -0.022445 #4: 0.003817 -0.003731 0.86 #5: -0.011407 -0.007491 -0.018898 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] adding rows without loops
Using the data generated with your code below, does rbind( DF1, DF2[ !(DF2$X.TIME %in% DF1$X.TIME), ] ) DF1 - DF1[ order( DF1$X.DATE, DF1$X.TIME ), ] do the job? Rgds, Rainer On Thursday 23 May 2013 05:54:26 Adeel - SafeGreenCapital wrote: Thank you Blaser: This is the exact solution I came up with but when comparing 8M rows even on an 8G machine, one runs out of memory. To run this effectively, I have to break the DF into smaller DFs, loop through them and then do a massive rmerge at the end. That's what takes 8+ hours to compute. Even the bigmemory package is causing OOM issues. -Original Message- From: Blaser Nello [mailto:nbla...@ispm.unibe.ch] Sent: Thursday, May 23, 2013 12:15 AM To: Adeel Amin; r-help@r-project.org Subject: RE: [R] adding rows without loops Merge should do the trick. How to best use it will depend on what you want to do with the data after. The following is an example of what you could do. This will perform best, if the rows are missing at random and do not cluster. DF1 - data.frame(X.DATE=rep(01052007, 7), X.TIME=c(2:5,7:9)*100, VALUE=c(37, 42, 45, 45, 45, 42, 45), VALE2=c(29,24,28,27,35,32,32)) DF2 - data.frame(X.DATE=rep(01052007, 7), X.TIME=c(2:8)*100, VALUE=c(37, 42, 45, 45, 45, 42, 45), VALE2=c(29,24,28,27,35,32,32)) DFm - merge(DF1, DF2, by=c(X.DATE, X.TIME), all=TRUE) while(any(is.na(DFm))){ if (any(is.na(DFm[1,]))) stop(Complete first row required!) ind - which(is.na(DFm), arr.ind=TRUE) prind - matrix(c(ind[,row]-1, ind[,col]), ncol=2) DFm[is.na(DFm)] - DFm[prind] } DFm Best, Nello -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Adeel Amin Sent: Donnerstag, 23. Mai 2013 07:01 To: r-help@r-project.org Subject: [R] adding rows without loops I'm comparing a variety of datasets with over 4M rows. I've solved this problem 5 different ways using a for/while loop but the processing time is murder (over 8 hours doing this row by row per data set). As such I'm trying to find whether this solution is possible without a loop or one in which the processing time is much faster. Each dataset is a time series as such: DF1: X.DATE X.TIME VALUE VALUE2 1 01052007 020037 29 2 01052007 030042 24 3 01052007 040045 28 4 01052007 050045 27 5 01052007 070045 35 6 01052007 080042 32 7 01052007 090045 32 ... ... ... n DF2 X.DATE X.TIME VALUE VALUE2 1 01052007 020037 29 2 01052007 030042 24 3 01052007 040045 28 4 01052007 050045 27 5 01052007 060045 35 6 01052007 070042 32 7 01052007 080045 32 ... ... n+4000 In other words there are 4000 more rows in DF2 then DF1 thus the datasets are of unequal length. I'm trying to ensure that all dataframes have the same number of X.DATE and X.TIME entries. Where they are missing, I'd like to insert a new row. In the above example, when comparing DF2 to DF1, entry 01052007 0600 entry is missing in DF1. The solution would add a row to DF1 at the appropriate index. so new dataframe would be X.DATE X.TIME VALUE VALUE2 1 01052007 020037 29 2 01052007 030042 24 3 01052007 040045 28 4 01052007 050045 27 5 01052007 060045 27 6 01052007 070045 35 7 01052007 080042 32 8 01052007 090045 32 Value and Value2 would be the same as row 4. Of course this is simple to accomplish using a row by row analysis but with of 4M rows the processing time destroying and rebinding the datasets is very time consuming and I believe highly un-R'ish. What am I missing? Thanks! [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ 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] formatting column names of data frame
Have you tried xtable? library( xtable ) x - structure(list(Record = 1:3, Average = c(34L, 14L, 433L), Maximum = c(899L, 15L, 1003L)), .Names = c(Record, Average, Maximum), class = data.frame, row.names = c(NA, -3L)) x - xtable( x ) print( x ) % latex table generated in R 2.15.2 by xtable 1.7-1 package % Fri May 17 22:22:00 2013 \begin{table}[ht] \centering \begin{tabular}{} \hline Record Average Maximum \\ \hline 11 34 899 \\ 22 14 15 \\ 33 433 1003 \\ \hline \end{tabular} \end{table} On Friday 17 May 2013 12:53:12 Patrick Leyshock wrote: Is there any way to format the headers of data frames, for printing? I am using Sweave to generate formatted reports. In Sweave, I read in a data.frame: result - read.table(path.to.table); then display it: print.data.frame(result); This gives me what I expect in the eventual final output: RecordAverage Maximum 1 34 899 2 14 15 3 433 1003 ... ... ... What I am hoping to do is distinguish one or more of the column headers, for example, I want Average or Maximum to be bold, underlined, etc., just some way to make the column name stand out visually. Any idea if this is possible? Any suggestions appreciated. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Merge two dataframe with by, and problems with the common field
Not sure whether this really helps you but at least it works for your sample: d3 - merge( d1, d2, by = c( a, b ) ) d3 a b c d f 1 1 4 5 6 8 2 2 5 6 7 9 3 3 6 7 8 10 Rgds, Rainer On Tuesday 07 May 2013 14:33:12 jpm miao wrote: Hi, From time to time I merge two dataframes with possibly a common field. Then the common field is no longer present,but what are present fieldname.x and fieldname.y. How can I fix the problem so that I can still call by the orignal fieldname? If you don't understand my problem, please see the example below. Thanks Miao d1 a b c 1 1 4 5 2 2 5 6 3 3 6 7 d2 d a f b 1 6 1 8 4 2 7 2 9 5 3 8 3 10 6 d3-merge(d1, d2, by=b) d3 b a.x c d a.y f 1 4 1 5 6 1 8 2 5 2 6 7 2 9 3 6 3 7 8 3 10 d3[a] Error in `[.data.frame`(d3, a) : undefined columns selected d3[a.x] a.x 1 1 2 2 3 3 [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Missing data
I read your data into a dataframe x - read.table( clipboard ) and renamed the only column colnames( x )[1] - orig With a loop, I created a 2nd column miss where in every 10th row the observation is set to NA: for( i in 1 : length( x$orig ) ) { if( as.integer( rownames( x )[ i ] ) %% 10 == 0 ) { x$miss[i] - NA } else { x$miss[i] - x$orig[i] } } This is probably the least elegant of all possible solutions but it works... Rgds, Rainer On Wednesday 24 April 2013 23:41:21 Roslina Zakaria wrote: Dear r-users, I would like to investigate about how to fill in missing data. I started with a complete data and try to introduce missing data into the data series. Then I would use some method to fill in the missing data and then compare with the original data how good it is. My question is, how do I introduce missing data in my complete data systematically like for example every 10th data will be erased and assumed as missing. Here are some rainfall data: 125 130.3 327.2 252.2 33.8 6.1 5.1 0.5 0.5 0 2.3 0 0 0 0 0 0 0 0 0 0.8 5.1 0 0.3 0 0 0 0 0 0 45.7 43.4 0 0 0 0 0 Thank you so much for any help given. I hope my question is clear. [[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] Replace missing value within group with non-missing value
Probably not very R-ish but it works (your data in a dataframe called x), if I understand your question right: # replace NA with 0 x$mth - ifelse( is.na( x$mth ), 0, x$mth ) # loop through observation numbers and replace 0 with the month no for( i in unique( x$obs ) ) x$mth[ x$obs == i ] - max( x$mth[ x$obs == i ] ) Rgds, Rainer x dn obs choice br mth 1 4 1 0 1 487 2 4 1 0 2 487 3 4 1 0 3 487 4 4 1 0 4 487 5 4 1 0 5 487 6 4 1 1 6 487 7 4 2 0 1 488 8 4 2 0 2 488 9 4 2 1 3 488 10 4 2 0 4 488 11 4 2 0 5 488 12 4 2 0 6 488 13 4 3 0 1 488 14 4 3 0 2 488 15 4 3 0 3 488 16 4 3 0 4 488 17 4 3 0 5 488 18 4 3 1 6 488 19 4 4 0 1 489 20 4 4 0 2 489 21 4 4 1 3 489 22 4 4 0 4 489 23 4 4 0 5 489 24 4 4 0 6 489 25 4 5 0 1 489 26 4 5 0 2 489 27 4 5 0 3 489 28 4 5 0 4 489 29 4 5 0 5 489 30 4 5 1 6 489 31 4 6 0 1 489 32 4 6 0 2 489 33 4 6 0 3 489 34 4 6 0 4 489 35 4 6 0 5 489 36 4 6 1 6 489 37 4 7 0 1 490 38 4 7 0 2 490 39 4 7 0 3 490 40 4 7 0 4 490 41 4 7 0 5 490 42 4 7 1 6 490 43 4 8 0 1 491 44 4 8 0 2 491 45 4 8 0 3 491 46 4 8 0 4 491 47 4 8 0 5 491 48 4 8 1 6 491 49 4 9 0 1 0 50 4 9 0 2 0 On Saturday 06 April 2013 16:16:16 Leask, Graham wrote: Hi Rui, Data as follows structure(list(dn = c(4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4), obs = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9), choice = c(0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0), br = c(1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2), mth = c(NA, NA, NA, NA, NA, 487, NA, NA, 488, NA, NA, NA, NA, NA, NA, NA, NA, 488, NA, NA, 489, NA, NA, NA, NA, NA, NA, NA, NA, 489, NA, NA, NA, NA, NA, 489, NA, NA, NA, NA, NA, 490, NA, NA, NA, NA, NA, 491, NA, NA)), .Names = c(dn, obs, choice, br, mth), row.names = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50), class = data.frame) Best wishes Graham -Original Message- From: Rui Barradas [mailto:ruipbarra...@sapo.pt] Sent: 06 April 2013 16:32 To: Leask, Graham Cc: r-help@r-project.org Subject: Re: [R] Replace missing value within group with non-missing value Hello, Can't you post a data example? If your dataset is named 'dat' use dput(head(dat, 50)) # paste the output of this in a post Rui Barradas Em 06-04-2013 15:34, Leask, Graham escreveu: Hi Rui, Thank you for your suggestion which is very much appreciated. Unfortunately running this code produces the following error. error in '$-.data.frame' ('*tmp*', mth, value = NA_real_) : replacement has 1 rows, data has 0 I'm sure there must be an elegant solution to this problem? Best wishes Graham On 6 Apr 2013, at 12:15, Rui Barradas ruipbarra...@sapo.pt wrote: Hello, That's not a very good way of posting your data, preferably paste the output of ?dput in a post. Some thing along the lines of the following might do what you want. It seems that the groups are established by 'dn' and 'obs' numbers. If so, try # Make up some data dat - data.frame(dn = 4, obs = rep(1:5, each = 6), mth = NA) dat$mth[6] - 487 dat$mth[9] - 488 dat$mth[18] - 488 dat$mth[21] - 489 dat$mth[30] - 489 sp - split(dat, list(dat$dn, dat$obs)) names(sp) - NULL tmp - lapply(sp, function(x){ idx - which(!is.na(x$mth))[1] x$mth - x$mth[idx] x }) do.call(rbind, tmp) Hope this helps, Rui Barradas Em 06-04-2013 11:33, Leask, Graham escreveu: Dear List members I have a large dataset organised in choice groups see sample below +-+ | dn obs choice acid br date cdate situat~n mth year set | |-| 1. | 4 10 LOSEC1. . . . 1 | 2.
Re: [R] Can R read open office.org Calc files
This might help: http://www.omegahat.org/ROpenOffice/ Rgds, Rainer On Thursday 28 March 2013 17:32:23 Shane Carey wrote: Hi, Can R read open office.org Calc files Thanks __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Combinations
Is ?expand.grid what you are looking for? Rgds, Rainer On Friday 15 March 2013 09:22:15 Amir wrote: Hi every one, I have two sets T1={c1,c2,..,cn} and T2={k1,k2,...,kn}. How can I find the sets as follow: (c1,k1), (c1,k2) ...(c1,kn) (c2,k1) (c2,k2) (c2,kn) ... (cn,kn) Thanks. Amir __ 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 can I read the following complicated table
What have you tried so far that did not work, and what do you want the result of your reading the text file look like? What is store somewhere? Why does myDF - read.table( myData.txt ) which gives you myDF V1 V2 V3 V4 1Monday 12 78 89 2 Tuesday 34 44 67 3 Wednesday 78 98 2 4 Thursday 34 55 4 as a starting point, not suffice? Rgds, Rainer On Friday 14 December 2012 10:50:56 jpm miao wrote: Hello, I have a table (in a txt file) which look like this: Monday 12 78 89 Tuesday 34 44 67 Wednesday 78 98 2 Thursday 34 55 4 Then the table repeats Monday , Tuesday, ... followed by several numbers My goal is to read values after the table. My problem is a little more complicated, but I just present a simpler case for ease of illustration. Is there any way to ask R to read several number after you see the word 'Monday' and store somewhere, and read several number after you see the word 'Tuesday' and store somewhere? Thanks, miao [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] help with if statement
Does A$TIME - ifelse( A$TIME = 24, A$TIME + 24, A$TIME ) what you want? Rgds, Rainer On Wednesday 21 November 2012 09:05:39 york8866 wrote: Hi all, I had a dataset A like: TIME DV 0 0 1 10 520 24 30 36 80 48 60 72 15 I would like to add 24 to those values higher than 24 in the TIME column. I did the following: If (A$TIME=24) { A$TIME - A$TIME+24} It did not work. How should I do it? Thanks, -- View this message in context: http://r.789695.n4.nabble.com/help-with-if-statement-tp4650315.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Scaling values 0-255 - -1 , 1 - how can this be done?
x - as.data.frame( matrix( 0:255, nrow = 16 ) ) ifelse( x 127.5, 1, -1 ) Is that what you want? Rgds, Rainer On Wednesday 21 November 2012 10:32:49 Brian Feeny wrote: I have a dataframe in which I have values 0-255, I wish to transpose them such that: if value 127.5 value = 1 if value 127.5 value = -1 I did something similar using the binarize function of the biclust package, this transforms my dataframe to 0 and 1 values, but I wish to use -1 and 1 and looking for a way in R to do this. Brian __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Using cbind to combine data frames and preserve header/names
Not sure where the problem is? Since you did not provide sample data, I took the iris data set and converted it to your structure: x - cbind( iris[5], iris[1:3] ) head( x ) Species Sepal.Length Sepal.Width Petal.Length 1 setosa 5.1 3.5 1.4 2 setosa 4.9 3.0 1.4 3 setosa 4.7 3.2 1.3 4 setosa 4.6 3.1 1.5 5 setosa 5.0 3.6 1.4 6 setosa 5.4 3.9 1.7 Does that look like your data? If so, xbin - cbind( x[1], binarize( x[2:4] ) ) gives a result that should look just like what you want: head( xbin ) Species Sepal.Length Sepal.Width Petal.Length 1 setosa1 00 2 setosa1 00 3 setosa1 00 4 setosa1 00 5 setosa1 00 6 setosa1 00 Using xbin - cbind( x$Species, binarize( x[-1] ) ) doesn't make a difference. Or did I not understand your problem well? Rgds, Rainer On Saturday 17 November 2012 00:39:02 Brian Feeny wrote: I have a dataframe that has a header like so: class value1 value2 value3 class is a factor the actual values in the columns value1, value2 and value3 are 0-255, I wish to binarize these using biclust. I can do this like so: binarize(dataframe[,-1]) this will return a dataframe, but then I lose my first column class, so I thought I could combine it like so: dataframe - cbind(dataframe$label, binarize(dataframe[,-1])) but then I lose my header (names).how can I do the above operation and keep my header in tact? Basically i just want to binarize everything but the first column (since its a factor column and not numeric). Thank you for any help you can give me, I am relatively new to R. Brian __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] summation coding
Assuming that you actually mean a1(b2+b3+b4) + a2(b1+b3+b4) + a3(b1+b2+b4) + a4(b1+b2+b3) ^ ^ ^ this might give you what you want: x - data.frame( a = sample( 1:10, 4 ), b = sample( 11:20, 4 ) ) x a b 1 1 16 2 7 15 3 8 19 4 4 13 for( i in 1 : length( x$a ) ) x$c[i] - x$a[i] * ( sum( x$b ) - x$b[i] ) x a b c 1 1 16 47 2 7 15 336 3 8 19 352 4 4 13 200 Rgds, Rainer On Thursday 18 October 2012 12:33:39 djbanana wrote: I would like to code the following in R: a1(b1+b2+b3) + a2(b1+b3+b4) + a3(b1+b2+b4) + a4(b1+b2+b3) or in summation notation: sum_{i=1, j\neq i}^{4} a_i * b_i I realise this is the same as: sum_{i=1, j=1}^{4} a_i * b_i - sum_{i=j} a_i * b_i would appreciate some help. Thank you. -- View this message in context: http://r.789695.n4.nabble.com/summation-coding-tp4646678.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] summation coding
You asked for a loop, you got one... Vectorized is easier and faster: x$c - x$a * ( sum( x$b ) - x$b ) Rgds, Rainer On Friday 19 October 2012 09:38:35 you wrote: Hi, I think I solved it myself by writing loops. What I meant is: are there in-built functions in R that calculate the following: a1(b2+...+b190) + a2(b1+b3+...+b190) + ... I managed to solve it, quite similar to what you just emailed. Thanks! On 19 October 2012 09:20, Rainer Schuermann rainer.schuerm...@gmx.netwrote: Is it possible that you mean a1(b2+b3+b4) + a2(b1+b3+b4) + a3(b1+b2+b4) + a4(b1+b2+b3) ^ ^ ^ On Thursday 18 October 2012 12:33:39 djbanana wrote: I would like to code the following in R: a1(b1+b2+b3) + a2(b1+b3+b4) + a3(b1+b2+b4) + a4(b1+b2+b3) or in summation notation: sum_{i=1, j\neq i}^{4} a_i * b_i I realise this is the same as: sum_{i=1, j=1}^{4} a_i * b_i - sum_{i=j} a_i * b_i would appreciate some help. Thank you. -- View this message in context: http://r.789695.n4.nabble.com/summation-coding-tp4646678.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] unique
If I understand your problem correctly (as Milan has pointed out, sample data and code would help enormously) this should get you where you want: unique( shopdata$name[ shopdata$employee 10 ] ) If not, something is wrong from the outset (or with my understanding, but then ... see above)! Rgds, Rainer On Tuesday 16 October 2012 14:45:37 paladini wrote: Hello everybody, I've got a problem concerning the function unique. I have got a data.frame shopdata with 1000 shop which were evaluated at different points in time. With function subset I chose those shops with more then 10 employee and store it in data.frame bigshopdata with 700 shops. bigshopdata=subset(shopdata, shopdata$employee10) Now I use unique(bigshopdata$name) to ensure that each shop name is only listed ones and not two or three times because of an evaluation at different dates. What happens is that unique eliminates also those shops names which appear only ones in bigshopdata but twice or more often in shopdata. But that is not what I want to. I'm only interessted in multiple appearance in bigshopdata not in an possible multible appearance in the original data.farme. How can I use unique to get what I want? Or is there an alternative function? I hope I explained the problem good enought. Best regards Claudia __ 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. - - - - - Boycott Apple! http://i.eatliver.com/2012/9362.jpg http://blogs.computerworlduk.com/simon-says/2012/09/iphone-5-misses-standardisation-opportunity/index.htm __ 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 Rename Column Labels?
You can't do that on disk - try: dfr - read.table ( /Users/MAC/Desktop/data.txt ) dfr A1 A2 A3 A4 A5 1 33 44 55 66 77 colnames( dfr ) - c( Mike, Kate, Michelle, Paul, Young ) dfr Mike Kate Michelle Paul Young 1 33 44 55 6677 write.table( dfr, /Users/MAC/Desktop/data.txt ) Rgds, Rainer On Friday 07 September 2012 22:54:32 Youn Se Hwan wrote: Hi, How do I rename the column labels in the table? For Instance, if I have a table like this, and I want to have the column labels changed from A1 A2 A3 A4 A5 to Mike Kate Michelle Paul Young A1 A2 A3A4A5 1 3344 55 6677 2 3 4 5 6 7 7 8 9 and my text file location is: /Users/MAC/Desktop/data.txt When I type in colnames(data.txt)[1] - Income, I get an error message saying target of assignment expands to non-language object. Thanks! __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. - - - - - Boycott Apple! __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help on finding specific columns in matrix
If I understand your question correctly, you want to identify the one column that has the lowest mean of all columns, and the one column that has the highest mean of all columns. Using your provided sample data, this gives you the indices: colMeans(a) [1] 12.48160 17.46868 22.51761 27.59880 which.min( colMeans( a ) ) [1] 1 which.max( colMeans( a ) ) [1] 4 Does that help? Rgds, Rainer On Sunday 02 September 2012 02:49:11 Andras Farkas wrote: Dear All, I have a matrix with 33 columns and 5000 rows. I would like to find 2 specific columns in the set: the one that holds the highest values and the one that holds the lowest values. In this case the column's mean would be apropriate to use to try to find those specific columns because each columns mean is different and they all change together based on the same change of rate constants as a function of time (ie: tme is the most important determinant of the magnitude of that mean). The columns are not to be named, if possible Any thoughts on that? Would apreciate the help example: a - matrix(c(runif(500,10,15),runif(500,15,20),runif(500,20,25),runif(500,25,30)),ncol=4) I would need to find and plot with a box percentile plot column 1, the column with the lowest mean, and column 4, the column with the highest mean thanks, Andras [[alternative HTML version deleted]] - - - - - Boycott Apple! __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Finding the last value before a certain date
dat - structure(list(date = structure(c(14879, 14886, 14893, 14899, 14906, 14913), class = Date), y = c(1356L, 1968L, 2602L, 3116L, 3496L, 3958L)), .Names = c(date, y), row.names = c(1, 2, 3, 4, 5, 6), class = data.frame) x - as.Date( 2010-10-06 ) getY - function( x ) { dat[ dat$date = x, 2][ length( dat[ dat$date x, 2] ) ] } getY( x ) [1] 1968 getY( as.Date( 2010-10-17 ) ) [1] 2602 Is that what you want? Rgds, Rainer Original-Nachricht Datum: Thu, 19 Jul 2012 09:42:05 +0200 Von: Robert Latest boblat...@gmail.com An: Betreff: [R] Finding the last value before a certain date Hello all, I have a dataframe that looks like this: head(df) datey 1 2010-09-27 1356 2 2010-10-04 1968 3 2010-10-11 2602 4 2010-10-17 3116 5 2010-10-24 3496 6 2010-10-31 3958 I need a function that, given any date, returns the y value corresponding to the given date or the last day before the given date. Example: Input: as.Date(2010-10-06). Output: 1968 (because the last value is from 2010-10-04) I've been tinkering with this for an hour now, without success. I think the solution is either surprisingly complicated or surprisingly simple. Thanks, robert __ 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. -- --- Gentoo Linux with KDE __ 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] Truncating (rounding down) to nearest half hour.
My amateur approach: I put your data in a dataframe called t: head( t ) Date Score 1 2008-05-01 08:58:0080 2 2008-05-01 13:31:0011 3 2008-05-01 16:35:0081 4 2008-05-01 23:20:00 152 5 2008-05-02 01:01:00 130 6 2008-05-02 03:35:00 122 Then I created a vector with rounded down minutes: minutes - floor( as.numeric( substr( t[,1], 15, 16 ) ) / 30 ) * 30 minutes - ifelse( minutes == 30, 30, 00 ) head( minutes ) [1] 30 30 30 00 00 30 Next the replacement: tc - t tc[,1] - as.character( t[,1] ) substr( tc[,1] , 15, 16 ) - minutes tc[,1] - as.POSIXct( tc[,1] ) head( tc ) Date Score 1 2008-05-01 08:30:0080 2 2008-05-01 13:30:0011 3 2008-05-01 16:30:0081 4 2008-05-01 23:00:00 152 5 2008-05-02 01:00:00 130 6 2008-05-02 03:30:00 122 Is that what you were looking for? Rgds, Rainer Original-Nachricht Datum: Thu, 19 Jul 2012 10:03:23 -0700 (PDT) Von: APOCooter mikeedinge...@gmail.com An: r-help@r-project.org Betreff: [R] Truncating (rounding down) to nearest half hour. I couldn't find anything in the chron or timeDate packages, and a good search yielded rounding to the nearest half hour, which I don't want. The data: structure(list(Date = structure(c(1209625080, 1209641460, 1209652500, 1209676800, 1209682860, 1209692100, 1209706980, 1209722580, 1209726300, 1209739620, 1209762780, 1209765720, 1209770520, 1209791040, 1209812580, 1209829920, 1209837180, 1209848160, 1209854640, 1209859440, 1209870780, 1209887760, 1209901080, 1209921660, 1209929280, 1209945600, 1209957240, 1209980280, 1210001760, 1210017000, 1210021140, 1210034820, 1210042800, 1210048980, 1210061520, 1210074480, 1210081200, 1210089300, 1210095960, 1210104120, 1210110900, 1210110900, 1210118400, 1210126980, 1210134180, 1210142640, 1210156080, 1210164180, 1210176840, 1210183740), class = c(POSIXct, POSIXt), tzone = ), Score = c(80L, 11L, 81L, 152L, 130L, 122L, 142L, 20L, 1L, 31L, 93L, 136L, 128L, 112L, 48L, 57L, 92L, 108L, 100L, 107L, 81L, 37L, 47L, 70L, 114L, 125L, 99L, 46L, 108L, 106L, 111L, 75L, 75L, 136L, 36L, 13L, 35L, 71L, 105L, 113L, 116L, 116L, 94L, 130L, 102L, 19L, 1L, 33L, 78L, 89L)), .Names = c(Date, Score), row.names = c(NA, 50L), class = data.frame) I'm trying to round the time down to the nearest half hour, without losing the date. For example, 01/01/2009 1:29 would round to 01/01/2009 1:00, while 01/01/2009 1:31 would round to 01/01/2009 1:30. Any help is greately appreciated. -- View this message in context: http://r.789695.n4.nabble.com/Truncating-rounding-down-to-nearest-half-hour-tp4637083.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. -- --- Gentoo Linux with KDE __ 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] Last answer
Is that what you mean: 2 + 3 [1] 5 .Last.value [1] 5 Rgds, Rainer (from R-intro.pdf, page 7, 2nd footnote) Original-Nachricht Datum: Thu, 19 Jul 2012 22:12:22 -0700 (PDT) Von: darnold dwarnol...@suddenlink.net An: r-help@r-project.org Betreff: [R] Last answer Hi, In Matlab, I can access the last computation as follows: 2+3 ans = 5 ans ans = 5 Anything similar in R? David -- View this message in context: http://r.789695.n4.nabble.com/Last-answer-tp4637151.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. -- --- Gentoo Linux with KDE -- --- Gentoo Linux with KDE __ 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] please help! Extract the row to the new file by using if-statment
Not sure whether I understand your data and objectives well enough but here is what I would do: To make my life easier, I used x as a variable name. I'm not using attach(). You can extract your data with something like y - x[x$wrfta= 255 | x$wrfta= 65 x$wrfrain == 0, ] y - y[!is.na(y[5]),] y Date wrfRH wrfsolar wrfwindspeed wrfrain wrftdwrfta 1 21/10/2010 92.9722.1153.27 0 1546.3379 61.00853 2 22/10/2010 87.3521.9940.89 0 1300.4083 62.85352 7 27/10/2010 89.0021.6642.30 0 1060.4449 41.86858 9 29/10/2010 84.5021.6637.80 0 1015.8014 34.11626 10 30/10/2010 84.9822.0036.27 0 839.5041 43.44048 11 31/10/2010 84.4022.4033.44 0 742.5285 45.81573 12 1/11/2010 80.0922.2438.35 0 1157.9933 45.59035 13 2/11/2010 84.4121.6936.19 0 1075.2672 51.66310 14 3/11/2010 88.5521.2237.73 0 1163.2865 51.34180 18 7/11/2010 89.4520.8124.75 0 720.9882 57.76271 19 8/11/2010 85.8220.9628.63 0 790.5736 37.96772 20 9/11/2010 85.0220.9631.94 0 703.2994 40.62208 Does that help? Rgds, Rainer By the way, it is better to provide the data in dput() format: x - structure(list(Date = structure(c(2L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 1L, 3L, 14L, 15L, 16L, 17L, 18L, 19L, 20L ), .Label = c(1/11/2010, 21/10/2010, 2/11/2010, 22/10/2010, 23/10/2010, 24/10/2010, 25/10/2010, 26/10/2010, 27/10/2010, 28/10/2010, 29/10/2010, 30/10/2010, 31/10/2010, 3/11/2010, 4/11/2010, 5/11/2010, 6/11/2010, 7/11/2010, 8/11/2010, 9/11/2010), class = factor), wrfRH = c(92.97, 87.35, 88.38, 92.32, 93.42, 93.38, 89, NA, 84.5, 84.98, 84.4, 80.09, 84.41, 88.55, 90.58, 95.17, 95.2, 89.45, 85.82, 85.02), wrfsolar = c(22.11, 21.99, 21.71, 15.45, 21.59, 20.15, 21.66, NA, 21.66, 22, 22.4, 22.24, 21.69, 21.22, 2.88, 2.46, 11.18, 20.81, 20.96, 20.96), wrfwindspeed = c(53.27, 40.89, 28.04, 22.38, 35.5, 42.58, 42.3, NA, 37.8, 36.27, 33.44, 38.35, 36.19, 37.73, 38.49, 32.22, 27.55, 24.75, 28.63, 31.94), wrfrain = c(0, 0, 0.01, 0.51, 0.52, 0.07, 0, NA, 0, 0, 0, 0, 0, 0, 0.56, 3.48, 0.84, 0, 0, 0), wrftd = c(1546.337861, 1300.408288, 1381.768284, 1113.90981, 868.4895334, 1404.722837, 1060.444918, 1109.596721, 1015.801383, 839.5041209, 742.5284832, 1157.99328, 1075.26719, 1163.286504, 1022.03364, 1065.735327, 1027.066675, 720.9881913, 790.5735604, 703.2993511), wrfta = c(61.00852664, 62.85352227, 54.80594493, 39.46573663, 28.42952321, 40.29300856, 41.86858345, 39.84995092, 34.11625725, 43.44047866, 45.81572847, 45.59035293, 51.66310159, 51.34179935, 57.74352136, 57.7734991, 54.40282225, 57.76270824, 37.96771725, 40.62208274), cat = c(NA, NA, 1, 1, 1, 1, NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, 1, NA, NA, NA)), .Names = c(Date, wrfRH, wrfsolar, wrfwindspeed, wrfrain, wrftd, wrfta, cat), row.names = c(NA, -20L), class = data.frame) On Thursday 31 May 2012 07:55:01 pigpigmeow wrote: Dear all, I find some troubles about how to extact the row from csv. file by using if-statement condition. I want to extract the row if the rainfall is greater than the mean of rainfall and using the wrfta divided into 3 groups that's rainfall greater than mean - group A ( create file group A_rain) - groupB ( create file group B_rain) - groupC ( create file group C_rain) rainfall less than mean - group A ( create file group A_norain) - groupB ( create file group B_norain) - groupC ( create file group C_norain) my csv. file is .. DatewrfRH wrfsolarwrfwindspeedwrfrain wrftd wrfta 21/10/201092.97 22.11 53.27 0 1546.337861 61.00852664 22/10/201087.35 21.99 40.89 0 1300.408288 62.85352227 23/10/201088.38 21.71 28.04 0.011381.768284 54.80594493 24/10/201092.32 15.45 22.38 0.511113.90981 39.46573663 25/10/201093.42 21.59 35.50.52868.4895334 28.42952321 26/10/201093.38 20.15 42.58 0.071404.722837 40.29300856 27/10/201089 21.66 42.30 1060.444918 41.86858345 28/10/2010NA NA NA NA 1109.596721 39.84995092 29/10/201084.521.66 37.80 1015.801383 34.11625725 30/10/201084.98 22 36.27 0 839.5041209 43.44047866 31/10/201084.422.433.44 0 742.5284832 45.81572847 1/11/2010 80.09 22.24 38.35 0 1157.99328 45.59035293 2/11/2010 84.41 21.69 36.19 0 1075.26719 51.66310159 3/11/2010 88.55 21.22 37.73 0 1163.286504 51.34179935 4/11/2010
Re: [R] storing output of a loop in a matrix
Try for( i in 1:10 ){ ... } That should resove your problem 1.! Rgds, Rainer On Wednesday 23 May 2012 09:23:04 RH Gibson wrote: blap.txt is a numeric vector of length 64. I am using the following code: bd-scan(blap.txt) output-matrix(0,64,10) s-sum(bd) for (i in 10){ while (s0) {x1-sample(bd,(length(bd)-1), replace=F) s-sum(x1) bd-x1 output[i]-s } } write.table(output, file=res.txt) This code is not doing what I'd like it to do: 1. It is not running through the while loop 10 times, just once. 2. I can't work out how to get the results into the output matrix. The matrix should be 10 columns with the decreasing s values for each of the 10 runs through the for loop. Sorry for any obvious mistakes. I'm very new to R and teaching myself. Where am I going wrong here, please? Thank you. -- __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ 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] What's wrong with MEAN?
mean( 16, 18 ) [1] 16 mean( c( 16, 18 ) ) [1] 17 On Tuesday 22 May 2012 02:10:27 Vincy Pyne wrote: Dear R helpers, I have recently installed R version 2.15.0 I just wanted to calculate mean(16, 18) Surprisingly I got answer as mean(16, 18) [1] 16 mean(18, 16) [1] 18 mean(14, 11, 17, 9, 5, 18) [1] 14 So instead of calculating simple Arithmetic average, mean command is generating first element as average. I restarted the machine, changed the machine, but still the reply is same. I have been using this mean function ever since I strated learning R, but this has never happened. Kindly guide Vincy [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] how to do averaging of two tables (rows with columns)
Based upon my understanding of your problem, this should do the job: d3 - dat1 d3[2] - ifelse( d3[2] == 1, dat2[1,2], NA ) d3[3] - ifelse( d3[3] == 1, dat2[2,2], NA ) d3[4] - Nodata d3[5] - ifelse( d3[5] == 1, dat2[3,2], NA ) d3$average - rowMeans( d3[c(2,3,5)], na.rm = TRUE ) d3 is now the same as your dat3. This is cumbersome, but maybe a starting point. Rgds, Rainer On Friday 11 May 2012 01:35:18 Kristi Glover wrote: Hi R user, I saw some errors in the dat1. The correct dat1 is dat1 - structure(list(X = structure(1:4, .Label = c(Plot1, Plot2, plot3, plot4), class = factor), speciesX = c(1L, 0L, 0L, 1L), speciesY = c(0L, 1L, 0L, 0L), speciesZ = c(1L, 1L, 0L, 1L), speciesXX = c(1L, 0L, 1L, 0L)), .Names = c(X, speciesX, speciesY, speciesZ, speciesXX), class = data.frame, row.names = c(NA, -4L)) cheers, K From: kristi.glo...@hotmail.com To: r-help@r-project.org Date: Thu, 10 May 2012 19:03:38 -0300 Subject: [R] how to do averaging of two tables (rows with columns) Hi R user, I finally able to send you the table in readable format. I have seen that some of you do send tables in email when asking questions, but why i could not send. Any way some of you helped me to send you the example table in a readable format. now, I want to concentrate on my problem. I am trying to get the information (dat 3) from dat 1 and 2 in R. I have very big data but these data are just hypothetical data. my data structures are exactly same as dat 1 and dat 2. I created dat 3 and dat4 manually to show what information I wanted to have. I am struggling to figure it out how I can do in R. I think it is not difficult. I hope any one can help me. dat1 is the table of species occurrence (o means species absence, 1 means species presence). dat1 - structure(list(X = structure(1:4, .Label = c(Plot1, Plot2, plot3, plot4), class = factor), speciesX = c(1L, 0L, 1L, 0L), speciesY = c(0L, 1L, 0L, 0L), speciesZ = c(1L, 1L, 0L, 1L), speciesXX = c(0L, 0L, 1L, 0L)), .Names = c(X, speciesX, speciesY, speciesZ, speciesXX), class = data.frame, row.names = c(NA, -4L)) dat2 is the species tolerances value in each environmental variable dat2 - structure(list(X = structure(c(1L, 3L, 2L), .Label = c(SpeciesX, SpeciesXX, SpeciesY), class = factor), EnviA = c(0.21, 0.1, 0.14), EnviB = c(0.4, 0.15, 0.16), EnviC = c(0.17, 0.18, 0.19)), .Names = c(X, EnviA, EnviB, EnviC), class = data.frame, row.names = c(NA, -3L)) ## note (here in dat 2 there is no species Z you can see that ) Now, I want to get the average value of tolerances in each grid. like dat 3 the dat3 is based on the column EnviA. dat3 -structure(list(X = structure(1:4, .Label = c(plot1, plot2, plot3, plot4), class = factor), speciesX = c(0.21, NA, NA, 0.21), speciesY = c(NA, 0.1, NA, NA), speciesZ = structure(c(1L, 1L, 1L, 1L), .Label = Nodata, class = factor), speciesXX = c(0.14, NA, 0.14, NA), average = c(0.175, 0.1, 0.14, 0.21)), .Names = c(X, speciesX, speciesY, speciesZ, speciesXX, average), class = data.frame, row.names = c(NA, -4L)) dat4 is same thing as dat3 but here i used EnviB instead of EnviA. dat4 - structure(list(X = structure(1:4, .Label = c(plot1, plot2, plot3, plot4), class = factor), speciesX = c(0.4, NA, NA, 0.4), speciesY = c(NA, 0.15, NA, NA), speciesZ = structure(c(1L, 1L, 1L, 1L), .Label = Nodata, class = factor), speciesXX = c(0.16, NA, 0.16, NA), average = c(0.28, 0.15, 0.16, 0.4)), .Names = c(X, speciesX, speciesY, speciesZ, speciesXX, average), class = data.frame, row.names = c(NA, -4L)) I hope you understand my problem and you can help me. Thanks Kristi [[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-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] [Sweave] string.prefix without hyphen-minus
You cannot avoid the hyphen I think, but if you say (for example): prefix.string=foo/x then your files start with 'x-' (so 'graph' becomes 'foo/x-graph') which may be better for you than the hyphen at the beginning of the file name. Rgds, Rainer On Friday 04 May 2012 17:23:58 julia.jacob...@arcor.de wrote: Dear Sweave users, Could you help me to find a way to place Sweave output files in a subdirectory of the currentfolder without giving them a subname? If the option prefix.string=foo/ is used, all files are placed in this folder, but begin with an hyphen-minus, which makes it difficult to work with them. If the option prefix=FALSE is used, then files won't be placed in a subdirectory. Thanks in advance for your help, Julia __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Sweave: Avoiding recompilation of figures
The easiest way probably is to put the code that takes so long to execute, in a separate file, such as graph1.Rnw, and get it into your master file via SweaveInput( graph1.Rnw ). Once you are happy with your graph1, you can comment this line out and only compile the stuff that keeps changing. Of course, there are more general methods available: Check out http://cran.r-project.org/web/packages/cacheSweave/ Another way around re-compiling would be using make http://cgibbons.berkeley.edu/Research/Papers/MakefileTutorial.pdf As a modern replacement for Sweave http://cran.r-project.org/web/packages/knitr/index.html seems to be the thing to watch. Personally, I'm still putting up with some waiting time, rather than moving up the learning curve, but I'm sure one of these three will meet your requirement. Rgds, Rainer On Thursday 26 April 2012 16:29:28 julia.jacob...@arcor.de wrote: Hello everybody out there using Sweave, There are some complicated SQL queries and laborous calculations against large data included as R code chunks using Sweave in my LaTeX document. These code chunks create graphs that do not change most of the time, but they are of course recompiled every time I run Sweave on my file for update of minor changes to the LaTeX text. Is there an option implemented in Sweave allowing for avoidence of recompilation of already existing figures? Thanks in advance, Julia __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to reduce plot size on linux?
Cross posting http://stackoverflow.com/questions/10308955/how-to-reduce-image-size-in-sweave The code you have provided there is unusable, I assume your problems come from a lack of understanding how Sweave works (nothing to do with Linux). Always make sure that = and @ are always the first characters on the line, Comment character in R is # and % in Latex (never //). Have a look at Section 9 of this paper here http://www.stat.auckland.ac.nz/%7Eihaka/downloads/Sweave-customisation.pdf This should solve your problems with plot size in Sweave. Rgds, Rainer On Tuesday 24 April 2012 19:02:23 Manish Gupta wrote: Hi, I am working on linux and i need to reduce plot size (bar plot) so that i can easily use in sweave. How can i implement it? Regards -- View this message in context: http://r.789695.n4.nabble.com/How-to-reduce- plot-size-on-linux-tp4585371p4585371.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] recommended way to group function calls in Sweave
What I usually do when I have to write a report with some functions I use multiple times is that I put them in a separate file (call it setup.Rnw or so). The first chunk there loads the libraries, sets initial variable values etc: echo=FALSE, results=hide= library( xtable ) d - iris ind - 1 @ Then I have a number of chunks that are not evaluated at this point: chunk_name_1,eval=FALSE,echo=FALSE= summary(d[ , ind]) cor(d[ , ind]) @ chunk_name_2,eval=FALSE,echo=FALSE= # produce a nice table from some data @ In my master file, I load these definitions first SweaveInput( setup.Rnw ) and from here, I can suse the named chunks almost like function calls, as you you describe below. The advantage (for me) is that I have only one place where I maintain the functions code, and only one line in the real document, rather than a lot of code, possibly distributed over the document.. Rgds, Rainer On Wednesday 25 April 2012 16:20:13 Liviu Andronic wrote: On Wed, Apr 25, 2012 at 3:41 PM, Duncan Murdoch murdoch.dun...@gmail.com wrote: I would use the last method, or if the calls were truly repetitive (i.e. always identical, not just the same pattern), use a named chunk. Labeled chunks are indeed what I was looking for [1]. As far as I understand, this is what Sweave functions (or are these macros?) look like: = d - iris ind - 1:2 @ sw= summary(d[ , ind]) cor(d[ , ind]) @ = d - iris ind - 2:4 sw @ Regards Liviu [1] vignette('Sweave', 'utils') __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Encoding of Sweave file error message
I also had the same problem. Being on Linux, I prefer Walmes' command line method but I found that putting \usepackage[utf8x]{inputenc} as the first instruction in the master .Rnw file also does the trick. No need to change anything after that in the file, at least not for me! Rgds, Rainer On Thursday 12 April 2012 14:13:06 Duncan Mackay wrote: At 12:03 12/04/2012, you wrote: I had the same problem! So, as I'm a linux user, I prefer use linux terminal. On terminal I type this to compile R CMD Sweave --encoding=utf-8 myfile.Rnw and the compilation is successful. Try to set the encoding option in Sweave(). Bests. Walmes. == Walmes Marques Zeviani LEG (Laboratório de Estatística e Geoinformação, 25.450418 S, 49.231759 W) Departamento de Estatística - Universidade Federal do Paraná fone: (+55) 41 3361 3573 VoIP: (3361 3600) 1053 1173 e-mail: mailto:wal...@ufpr.brwal...@ufpr.br twitter: @walmeszeviani homepage: http://www.leg.ufpr.br/%7Ewalmeshttp://www.leg.ufpr.br/~walmes linux user number: 531218 == Hi Walmes Thank you very much. That appears to be the problem. When I typed from the DOS terminal I have exactl ParasiteComb12.Rnw it compiled the tex file without any error messages. I have not really got into font encoding and reading the Sweave manual I thought that what I had done would be sufficient. I found an old note which gave a reference to http://tolstoy.newcastle.edu.au/R/e10/help/10/05/4725.html http://tolstoy.newcastle.edu.au/R/e10/help/10/05/4889.html but that appears to be specific. The ?Sweave and the Sweave manual appear to be more specific about the latex side. After having a look at iconvlist() and bearing in mind Duncan Murdoch's comments about windows I tried Sweave(D:/Cic/Sweave/Parasite/Comb/12/ParasiteComb12.Rnw, encoding = UTF-8) from the Rgui command window and compiled without any problems When I had a look at the tex file there were a few DOS Alt-248 (degree symbol ) within latex comments which were added last running R2.14 before updating Removing them and re running without the encoding argument brought things back to normal. I tried as a test \SweaveOpts{encoding=UTF8} but that appears not to work All I have to do now is to put the extra argument into my text editors clip library for Sweave for next time when I cannot solve things. Its been a long week ! Regards Duncan Duncan Mackay Department of Agronomy and Soil Science University of New England ARMIDALE NSW 2351 Email home: mac...@northnet.com.au [[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] Recode Variable
1. Some data structured the way you are using would have been helpful. I used Tal Galil's play data and set up a dataframe with the variable names you are using: structure(list(var1 = c(1, NA, NA, 4, 5, 6, 7, 8, 9, 10, 5), var2 = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5)), .Names = c(var1, var2), row.names = c(NA, -11L), class = data.frame) myData var1 var2 1 11 2NA2 3NA3 4 44 5 55 6 66 7 77 8 88 9 99 10 10 10 1155 2. The error message I get from your line of code is myData[myData$var1==5;var1]-NA Error: unexpected ';' in myData[myData$var1==5; Probably the semikolon is a typo? 3. If I understand your question correctly, the easiest answer I can find is ifelse(): myData$var1 - ifelse( myData$var1 == 5, NA, myData$var1 ) myData var1 var2 1 11 2NA2 3NA3 4 44 5NA5 6 66 7 77 8 88 9 99 10 10 10 11 NA5 Rgds, Rainer On Thursday 12 April 2012 11:08:45 David Studer wrote: Hello everybody, I know this is pretty basic stuff, but could anyone explain me how to recode a single value of a variable into a missing value? I used to do it like this: myData[myData$var1==5;var1]-NA # recode value 5 into NA But the column var1 already contains NAs, which results in the following error message: missing values are not allowed in subscripted assignments of data frames Thank you very much for any advice! David [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] cbind, data.frame | numeric to string?
cbind() works as well, but only if c is attached to the existing test variable: tst - cbind( test, c ) tst ab c 1 1 0.3 y1 2 2 0.4 y2 3 3 0.5 y3 4 4 0.6 y4 5 5 0.7 y5 str( tst ) 'data.frame': 5 obs. of 3 variables: $ a: num 1 2 3 4 5 $ b: num 0.3 0.4 0.5 0.6 0.7 $ c: Factor w/ 5 levels y1,y2,y3,..: 1 2 3 4 5 Not saying it is a good idea, though... Rainer On Tuesday 10 April 2012 11:38:51 R. Michael Weylandt wrote: Don't use cbind() -- it forces everything into a single type, here string, which in turn becomes factor. Simply, data.frame(a, b, c) Like David mentioned a few days ago, I have no idea who is promoting this data.frame(cbind(...)) idiom, but it's a terrible idea (albeit one that seems to be very frequent over the last few weeks) Michael On Tue, Apr 10, 2012 at 10:33 AM, Anser Chen anser.c...@gmail.com wrote: Complete newbie to R -- struggling with something which should be pretty basic. Trying to create a simple data set (which I gather R refers to as a data.frame). So a - c(1,2,3,4,5); b - c(0.3,0.4,0.5,0,6,0.7); Stick the two together into a data frame (call test) using cbind test - data.frame(cbind(a,b)) Seems to do the trick: test a b 1 1 0.3 2 2 0.4 3 3 0.5 4 4 0.6 5 5 0.7 Confirm that each variable is numeric: is.numeric(test$a) [1] TRUE is.numeric(test$b) [1] TRUE OK, so far so good. But, now I want to merge in a vector of characters: c - c('y1,y2,y3,y4,y5) Confirm that this is string: is.numeric(c); [1] FALSE cbind c into the data frame: test - data.frame(cbind(a,b,c)) Looks like everything is in place: test a b c 1 1 0.3 y1 2 2 0.4 y2 3 3 0.5 y3 4 4 0.6 y4 5 5 0.7 y5 Except that it seems as if the moment I cbind in a character vector, it changes numeric data to string: is.numeric(test$a) [1] FALSE is.numeric(test$b) [1] FALSE which would explain why the operations I'm trying to perform on elements of a and b columns are failing. If I look at the structure of the data.frame, I see that in fact *all* the variables are being entered as factors. str(test) 'data.frame': 5 obs. of 3 variables: $ a: Factor w/ 5 levels 1,2,3,4,..: 1 2 3 4 5 $ b: Factor w/ 5 levels 0.3,0.4,0.5,..: 1 2 3 4 5 $ c: Factor w/ 5 levels y1,y2,y3,..: 1 2 3 4 5 But, if I try test - data.frame(cbind(a,b)) str(test) 'data.frame': 5 obs. of 2 variables: $ a: num 1 2 3 4 5 $ b: num 0.3 0.4 0.5 0.6 0.7 a and b are coming back as numeric. So, why does cbind'ing a column of character variables change everything else? And, more to the point, what do I need to do to 'correct' the problem (i.e., stop this from happening). [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ 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 convert factors to numbers
I guess the problem starts with setting read.table(...dec = ,, ... ). The data in your file are with decimal point. Without that, it works just fine: f - http://r.789695.n4.nabble.com/file/n4498828/p_diarios.txt; df - read.table( f, header = TRUE ) log.Price - log(df$price) head( log.Price ) [1] 2.893700 3.262701 3.432373 3.545586 3.317816 3.387098 Rgds, Rainer On 2012-03-23 19:47, Natasha Stavros wrote: As.numeric(as.character(factor.level.to.convert)) On Mar 23, 2012 11:40 AM, sandrosonav...@gmail.com wrote: Hello, I am relatively new to using R. The text file contains the date and price . I want to read and manipulate the data in R. However, when I use read.table, it treats all of the data as factors and I do not know how to treat the data as numbers: http://r.789695.n4.nabble.com/file/n4498828/p_diarios.txt p_diarios.txt setwd (C:\\Users\\Sandro\\Dropbox\\R) data.precios- read.table (p_diarios.txt , header =TRUE , dec=,, sep=\t) Time- data.precios$time # 01.02.2004 - 12.05.2011 Price- data.precios$price # Historical spot price log.Price- log(data.precios$price) Error en Math.factor(c(12L, 126L, 213L, 342L, 160L, 186L, 219L, 37L, 54L, : log not meaningful for factors As you can see, I cannot calculate the price logarithms. Any help is appreciated. Sandro -- View this message in context: http://r.789695.n4.nabble.com/How-to-convert-factors-to-numbers- tp4498828p4498828.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] R (Bold font) and Latex
More information (reproducible code) is needed to address your specific situation, but in general, you change the value of a variable in R and take care of the formatting in LaTeX. You may want to look at the Hmisc package's Latex() function. I have not tried it, xtable serves me well, but from a casual look, more formatting can be done with Hmisc. There are more choices, see http://tolstoy.newcastle.edu.au/R/e17/help/12/02/3755.html Rgds, Rainer On Thursday 22 March 2012 18:42:13 Manish Gupta wrote: Great it works! But in my case i have to use text bf in loop (R). Since x is variable (row from file) which keeps on changing. How can i implement the above logic in loop. Regards -- View this message in context: http://r.789695.n4.nabble.com/R-Bold-font-and-Latex-tp4487535p4497610.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to change colnames in xtable?
You can change the column names of a data frame with colnames( df ) - c( my, data, frame ) and from here, xtable() is your friend (or at least mine...). Rgds, Rainer On Thursday 22 March 2012 01:13:18 Manish Gupta wrote: Hi, Can we change column names in latex table? Regards -- View this message in context: http://r.789695.n4.nabble.com/How-to-change-colnames-in-xtable-tp4494833p44 94833.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] R (Bold font) and Latex
For a small number of elements you could use \Sexpr{}, i.e. echo= FALSE= x-c(1,0,2,4) @ x\\ \textbf{\Sexpr{x[1]}}\\ \textbf{\Sexpr{x[2]}}\\ \textbf{\Sexpr{x[3]}}\\ \textbf{\Sexpr{x[4]}}\\ Rgds, Rainer On Monday 19 March 2012 20:03:47 Manish Gupta wrote: Hi, I am using R and latex for generating report. I need R result to be in bold face. For instance. x-c(1,0,2,4) I need to print its output in bold face. x *1 2 3 4* I attempted to use textbf{} but can not write R output inside it. How can i implement it. Thanks in advance. Regards -- View this message in context: http://r.789695.n4.nabble.com/R-Bold-font-and-Latex-tp4487535p4487535.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] R (Bold font) and Latex
Or, with a little less typing: echo= FALSE= x-c(1,0,2,4) @ x\\ \begin{textbf} \Sexpr{x[1]}\\ \Sexpr{x[2]}\\ \Sexpr{x[3]}\\ \Sexpr{x[4]}\\ \end{textbf} On Tuesday 20 March 2012 10:14:38 Rainer Schuermann wrote: For a small number of elements you could use \Sexpr{}, i.e. echo= FALSE= x-c(1,0,2,4) @ x\\ \textbf{\Sexpr{x[1]}}\\ \textbf{\Sexpr{x[2]}}\\ \textbf{\Sexpr{x[3]}}\\ \textbf{\Sexpr{x[4]}}\\ Rgds, Rainer On Monday 19 March 2012 20:03:47 Manish Gupta wrote: Hi, I am using R and latex for generating report. I need R result to be in bold face. For instance. x-c(1,0,2,4) I need to print its output in bold face. x *1 2 3 4* I attempted to use textbf{} but can not write R output inside it. How can i implement it. Thanks in advance. Regards -- View this message in context: http://r.789695.n4.nabble.com/R-Bold-font-and-Latex-tp4487535p4487535.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-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] a very simple question
As to the reasons, David as given you the necessary hints. In order to get around the issue, here is what I do: a - round( 0.1 * ( 1:9 ), 1 ) a [1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 which( a == 0.3 ) [1] 3 Rgds, Rainer Original-Nachricht Datum: Sun, 18 Mar 2012 21:43:54 + Von: Dajiang Liu ldjst...@hotmail.com An: r-help@r-project.org Betreff: [R] a very simple question Dear All, I have a seemingly very simple question, but I just cannot figure out the answer. I attempted to run the following:a=0.1*(1:9);which(a==0.3);it returns integer(0). But obviously, the third element of a is equal to 0.3. I must have missed something. Can someone kindly explain why? Thanks a lot. Regards,Dajiang [[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. -- --- Gentoo Linux with KDE __ 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 retrieve a column name of a data frame
colnames( df )[2] [1] c2 On Thursday 02 February 2012 07:31:33 ikuzar wrote: Hi, I 'd like to know how to retrieve a column name of a data frame. For instance : df = data.frame(c1=c('a','b'),c2=c(1,2)) df c1 c2 1 a 1 2 b 2 I would like to retrieve the column name which value is 2 (here, the column is c2) thanks for your help -- View this message in context: http://r.789695.n4.nabble.com/How-to-retrieve-a-column-name-of-a-data-frame -tp4351764p4351764.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] function: as.integer
as.numeric(A) [1] 4.4 1.9 4.1 Integers are just integers... On Tuesday 31 January 2012 12:21:48 Marion Wenty wrote: dear r-helpers, i created an object named A, which looks like this: A - c(4.4,1.9,4.1) now i needed to get numbers instead of characters and for this i used the function: as.integer(A) which resulted in: [1] 4 1 4 My question is, why the numbers are rounded or more impotantly if there is a way to keep the decimal numbers? Thank you very much for your help in advance! Marion [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] reshape dataframe to array (pivot table)
Hi, I wouldn't know how to fill the data into the array form you want, but you can get the aggregated data with dfm - melt( df ) dfc - cast( dfm, LOC ~ variable, sum ) dfc LOC SPEC1 SPEC2 1 123 5 2 223 2 3 3 0 0 Hope this helps as a first step! Rgds, Rainer On 2012-01-24 17:15, Johannes Radinger wrote: Hello, I would like to reshape a dataframe into an array. This is kind a similar task as Excel performs with a Pivot table. To illustrate it: LOC- factor(c(1,2,2,3,1,1)) SPEC1- c(0,0,23,0,12,11) SPEC2- c(1,2,0,0,0,4) df- data.frame(LOC,SPEC1,SPEC2) # original dataframe a- array(NA,dim=c(length(levels(LOC)),1,2),dimnames=list(levels(LOC),LOC,c(SPEC1,SPEC2))) #new array set up, how it should look like The final array is splitted by the SPEC (for each SPEC an new layer), and the LOC is collapsed so that there is only one line per level of LOC. The array should get populated with: 1) the sums 2) and presence/absence (can be easily calculated from sums) What is the best way to do that? I looked into reshape and apply...probably one of them is suitable but I don't know how...Or has that to be done with a loop? cheers, /johannes __ 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 Sweave to generate multiple documents
Coming from a different angle: The LaTeX beamer class comes with the capability to produce different sets of documents from the same master file (presentation, handout, article...). That could get you where you want. For sweave questions, you may want to look at http://stackoverflow.com/questions/tagged/sweave Rgds, Rainer On 2012-01-17 02:45, Michael Friendly wrote: On 1/16/2012 4:20 PM, Ramiro Barrantes wrote: Hello, I tried looking for a Sweave-specific list but didn't find one, nor did I find an answer via google, so will send this question to the general R list. Please feel free to point me in the right direction. I am using Sweave and would like to have a single .Rnw document that generates 1) a summary report, 2) a full report, 3) slides for a talk. I think my material lends itself to have it all coming from one master document because a lot of the plots, writings, and calculations are shared, but I would need Sweave to generate separate files with me somehow pointing to what goes where. Is this possible with Sweave? Following up on Duncan, I think you are making it too hard to insist that everything comes from a single .Rnw document, which would entail an awful lot of conditionals at the latex or R level. On the other hand, if you have large sections of the .Rnw that would be shared across the summary report, full report or slides, you can always put them in separate .Rnw files and then have very short master .Rnw files that input them, as appropriate, using \SweaveInput{sec1.Rnw} \SweaveInput{sec2.Rnw} ... __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] nice report generator?
... or perhaps some other CRAN package has already gone in this direction. The tableGrob function in the gridExtra package probably is one of them. Original-Nachricht Datum: Wed, 7 Dec 2011 19:29:54 -0500 Von: Gabor Grothendieck ggrothendi...@gmail.com An: Duncan Murdoch murdoch.dun...@gmail.com CC: r-help r-h...@stat.math.ethz.ch, Janko Thyson janko.thyson.rst...@googlemail.com Betreff: Re: [R] nice report generator? On Wed, Dec 7, 2011 at 5:58 PM, Duncan Murdoch murdoch.dun...@gmail.com wrote: On 11-12-07 5:43 PM, Gabor Grothendieck wrote: On Wed, Dec 7, 2011 at 5:26 PM, Richard M. Heibergerr...@temple.edu wrote: Michael, that is a challenge. I accept it and suggest that it be a contest on the R-help list. Please post a pdf file showing some (more than one) tables that you think look better in Excel than in LaTeX. I,and probably some others, will send our versions of the tables. I think a new email thread with an appropriate catchy title would be the way to do it. The problem is that its drop dead easy in Excel but its not readily accessible in R and latex (as opposed to the problem being raw capability). Simply exhibiting how to do it is not enough. What is needed is a function, accessible from CRAN, with a bunch of built in themes that are easy to apply: latex(DF, theme = cherry orchard) I don't believe you. Show us some examples. As I mentioned, its not just a matter of replicating the output. Its a matter of how easy it is to generate it. With Excel 2007 enter a table into the cells, select it and on the Home tab in the Styles group of the ribbon click on Format a Table. Select any of the templates that are presented and that's it. I have produced quite fancy tables with R and latex beyond what the above instructions could do in Excel but I did not regard that effort as easy although it did produce very nice looking tables. There is a new tables package that just appeared on CRAN in the last few days. I haven't seriously used it yet but perhaps it has some of these capabilities (or if not will evolve into providing not only the tabular content but also the presentation aspects that are important to some users) or perhaps some other CRAN package has already gone in this direction. -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- --- Gentoo Linux with KDE __ 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] bizarre seq() behavior?
You have a dot (not a comma) after 8: seq(2,8.1,length.out=3) ^ Rgds, Rainer On Wednesday 23 November 2011 06:59:12 Czerminski, Ryszard wrote: Is there any rational explanation for the bizarre seq() behavior below? seq(2,8.1, lenght.out=3) [1] 2 3 4 5 6 7 8 help(seq) seq(2,8,length.out=3) [1] 2 5 8 seq(2,8.1,length.out=3) [1] 2.00 5.05 8.10 Except maybe that it is early in the morning :) Best regards, Ryszard Ryszard Czerminski AstraZeneca Pharmaceuticals LP 35 Gatehouse Drive Waltham, MA 02451 USA 781-839-4304 ryszard.czermin...@astrazeneca.com - - Confidentiality Notice: This message is private and may ...{{dropped:11}} __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ 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] bizarre seq() behavior?
...and a spelling mistake in your first line (lenght instead of length)... On Wednesday 23 November 2011 06:59:12 Czerminski, Ryszard wrote: Is there any rational explanation for the bizarre seq() behavior below? seq(2,8.1, lenght.out=3) [1] 2 3 4 5 6 7 8 help(seq) seq(2,8,length.out=3) [1] 2 5 8 seq(2,8.1,length.out=3) [1] 2.00 5.05 8.10 Except maybe that it is early in the morning :) Best regards, Ryszard Ryszard Czerminski AstraZeneca Pharmaceuticals LP 35 Gatehouse Drive Waltham, MA 02451 USA 781-839-4304 ryszard.czermin...@astrazeneca.com - - Confidentiality Notice: This message is private and may ...{{dropped:11}} __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] problem in creating a dataframe
Being new to R myself, I always get trapped by factors. Taking the data you have provided, this worked for my understanding of your intention: x - rep( 0, 4 ) x [1] 0 0 0 0 df - data.frame( matrix( x, 1 ), stringsAsFactors = FALSE ) df X1 X2 X3 X4 1 0 0 0 0 is.character( df[1,1] ) [1] TRUE Rgds, Rainer On Monday 21 November 2011 22:21:54 arunkumar wrote: Hi I have a character class and i need to convert into dataframe data=(0,0,0,0) I want a dataframe with each one should under a separate column Please help me -- View this message in context: http://r.789695.n4.nabble.com/problem-in-creating-a-dataframe-tp4094676p 4094676.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Generating Sequence of Dates
n = 2 sort( rep( seq(as.Date(2000/1/1), by=month, length.out=3), n ) ) [1] 2000-01-01 2000-01-01 2000-02-01 2000-02-01 2000-03-01 [6] 2000-03-01 On Sunday 20 November 2011 22:16:36 arunkumar wrote: Hi. I need to generate a sequence of date, I need to generate date for a period. Each date should be generated n times. E.g seq(as.Date(2000/1/1), by=month, length.out=3) the output of this is 2000-01-01 2000-02-01 2000-03-01 But i need each date to be generated n times, if n =2 my output 2000-01-01 2001-01-01 2000-02-01 2000-02-01 2000-03-01 2000-03-01 -- View this message in context: http://r.789695.n4.nabble.com/Generating-Sequence-of-Dates-tp4090672p409 0672.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] xtable and sweave: caption placement problem
It works if you separate the print command and put the caption placement in the print command , see below: \documentclass[11pt,a4paper]{article} \usepackage{Sweave} \begin{document} = x = runif(100, 1, 10) y = 2 + 3 * x + rnorm(100) @ echo=FALSE,results=tex= library(xtable) p - (xtable(summary(lm(y~x)),align=r, caption=Summary statistics for the regression model, label=tab:summary, digits=2)) print( p, caption.placement=top ) @ \end{document} Rgds, Rainer On Friday 18 November 2011 09:33:07 ren...@vannieuwkoop.ch wrote: Dear All I am running Sweave with xtable and want to put the caption placement on top. But this does not work. Any idea what is going wrong? Here is an example that runs properly with the exception of the caption placement in the pdf-file. \documentclass[11pt,a4paper]{article} \usepackage{Sweave} \begin{document} = x = runif(100, 1, 10) y = 2 + 3 * x + rnorm(100) @ echo=FALSE,results=tex= library(xtable) print(xtable(summary(lm(y~x)), align=r, caption=Summary statistics for the regression model, caption.placement=top, label=tab:summary, digits=2)) @ \end{document} Renger _ Renger van Nieuwkoop Centre for Energy Policy and Economics, ETH Zürichbergstrasse 18 (ZUE) CH - 8032 Zürich +41 44 632 02 63 mailto: ren...@vannieuwkoop.ch blog.modelworks.ch __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] length of empty string
nchar() [1] 0 On Friday 18 November 2011 16:09:38 Jaensch, Steffen [TIBBE] wrote: Hi all, Can somebody explain why length() returns 1 and not 0? How do I test if a given string is the empty string? Thanks, Steffen. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] regular expression for selection
Does library( stringr ) str_extract( mena, m5[0-9] ) achieve what you are looking for? Rgds, Rainer On Monday 14 November 2011 10:22:09 Petr PIKAL wrote: Hi On 11/14/2011 07:45 PM, Petr PIKAL wrote: Dear all I am again (as usual) lost in regular expression use for selection. Here are my data: dput(mena) c(138516_10g_50ml_50c_250utes1_m53.00-_s1.imp, 138516_10g_50ml_50c_250utes1_m54.00_s1.imp, 138516_10g_50ml_50c_250utes1_m55.00_s1.imp, 138516_10g_50ml_50c_250utes1_m56.00_s1.imp, 138516_10g_50ml_50c_250utes1_m57.00_s1.imp, 138516_10g_50ml_50c_250utes1_m58.00_s1.imp, 138516_10g_50ml_50c_250utes1_m59.00_s1.imp) I want to select only values m foolowed by numbers from 53 to 59. I used sub(m5., , mena) which correctly selects those m53 - m59 values but, in contrary to my expectation, it replaced the selected values with specified replacement - in that case empty string. What I shall use if I want to get rid of all but m53-m59 from those strings? Hi Petr, How about: grep(m5,mena) It gives numeric values which tells me that there is a match in each string, but as a result I need only m53-m59 substrings. Regards Petr Jim __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Extracting data by row
On Friday 28 October 2011 18:04:59 Vinny Moriarty wrote: Thanks everyone for you help with my last question, and now I have one last one... Here is what I would do, based on my understanding of your question: # your data snippet as data frame x site time_localtime_utc reef_type_code sensor_type 16 2006-04-09 10:20:00 2006-04-09 20:20:00BAKsb39 26 2006-04-09 10:40:00 2006-04-09 20:40:00BAKsb39 36 2006-04-09 11:00:00 2006-04-09 21:00:00BAKsb39 46 2006-04-09 11:20:00 2006-04-09 21:20:00BAKsb39 56 2006-04-09 11:40:00 2006-04-09 21:40:00BAKsb39 66 2006-04-09 12:00:00 2006-04-09 22:00:00BAKsb39 76 2006-04-09 12:20:00 2006-04-09 22:20:00BAKsb39 86 2006-04-09 12:40:00 2006-04-09 22:40:00BAKsb39 96 2006-04-09 13:00:00 2006-04-09 23:00:00BAKsb39 sensor_depth_m temperature_c 1 2 29.63 2 2 29.56 3 2 29.51 4 2 29.53 5 10 29.57 6 2 29.60 7 2 29.66 8 10 29.68 9 2 29.68 # extracting all 10m depth sensors using subscripting Ten - x[x[sensor_depth_m]==10,] Ten site time_localtime_utc reef_type_code sensor_type 56 2006-04-09 11:40:00 2006-04-09 21:40:00BAKsb39 86 2006-04-09 12:40:00 2006-04-09 22:40:00BAKsb39 sensor_depth_m temperature_c 5 10 29.57 8 10 29.68 Does that help? Rgds, Rainer On Friday 28 October 2011 18:04:59 Vinny Moriarty wrote: Thanks everyone for you help with my last question, and now I have one last one... Here is a sample of my data in .csv format site,time_local,time_utc,reef_type_code,sensor_type,sensor_depth_m,temper ature_c 06,2006-04-09 10:20:00,2006-04-09 20:20:00,BAK,sb39, 2, 29.63 06,2006-04-09 10:40:00,2006-04-09 20:40:00,BAK,sb39, 2, 29.56 06,2006-04-09 11:00:00,2006-04-09 21:00:00,BAK,sb39, 2, 29.51 06,2006-04-09 11:20:00,2006-04-09 21:20:00,BAK,sb39, 2, 29.53 06,2006-04-09 11:40:00,2006-04-09 21:40:00,BAK,sb39, 10, 29.57 06,2006-04-09 12:00:00,2006-04-09 22:00:00,BAK,sb39, 2, 29.60 06,2006-04-09 12:20:00,2006-04-09 22:20:00,BAK,sb39, 2, 29.66 06,2006-04-09 12:40:00,2006-04-09 22:40:00,BAK,sb39, 10, 29.68 06,2006-04-09 13:00:00,2006-04-09 23:00:00,BAK,sb39, 2, 29.68 My goal was to extract all of the rows from a certain depth. Using the column sensor_depth_m to order my data by, I wanted all of the data from 10m. So this is what I wanted when I finished site,time_local,time_utc,reef_type_code,sensor_type,sensor_depth_m,temper ature_c 06,2006-04-09 11:40:00,2006-04-09 21:40:00,BAK,sb39, 10, 29.57 06,2006-04-09 12:40:00,2006-04-09 22:40:00,BAK,sb39, 10, 29.68 06,2006-04-09 13:00:00,2006-04-09 23:00:00,BAK,sb39, 10, 29.68 To pull out all of the data from a 10m sensor depth I came up with the code: Ten-dataTable1[(dataTable1$sensor_depth_m==10),] But when I run it I just get an extra column tacked onto the end like this site,time_local,time_utc,reef_type_code,sensor_type,sensor_depth_m,temper ature_c, sensor_depth_m 06,2006-04-09 10:20:00,2006-04-09 20:20:00,BAK,sb39, 2, 29.63,10 06,2006-04-09 10:40:00,2006-04-09 20:40:00,BAK,sb39, 2, 29.56,10 06,2006-04-09 11:00:00,2006-04-09 21:00:00,BAK,sb39, 2, 29.51,10 06,2006-04-09 11:20:00,2006-04-09 21:20:00,BAK,sb39, 2, 29.53,10 06,2006-04-09 11:40:00,2006-04-09 21:40:00,BAK,sb39, 10, 29.57,10 06,2006-04-09 12:00:00,2006-04-09 22:00:00,BAK,sb39, 2, 29.60,10 06,2006-04-09 12:20:00,2006-04-09 22:20:00,BAK,sb39, 2, 29.66,10 06,2006-04-09 12:40:00,2006-04-09 22:40:00,BAK,sb39, 10, 29.68,10 06,2006-04-09 13:00:00,2006-04-09 23:00:00,BAK,sb39, 10, 29.68,10 It seems pretty straight forward, I'm not sure what I am missing. Thanks V [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] converting commas to points
From ?read.csv: read.csv2( file, header = TRUE, sep = ;, quote=\, dec=,, fill = TRUE, comment.char=, ...) I think this is specifically set up for German decimal commas. Rgds, Rainer On Thursday 06 October 2011 17:39:46 Anna Lee wrote: Hello everyone! I work with a german excell version which uses commas instead of points for seperating decimal places. R work with points so in order to be able to save my excell tables without changing the commas to points, whenever I load a table I type in: read.table(..., dec = ,) only R puts the points into the wron places. For excample excell has a cell with the number: 0,09 so what R does, it writes the number as 0.9 which is wrong and makes my calculations become useless. Does anyone know this problem? Maby I made a mistake somewhere? I would be glad about your answers! Cheers, Anna __ 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] Populate a matrix
m - matrix( rep( y, length( x ) ), length( y ), length( x ) ) On Wednesday 05 October 2011 18:11:18 fernando.cabr...@nordea.com wrote: Hi guys I have vectors x - c(1,2,3,4) and y - c(4,3,9) and would like to generate a matrix which has 3 rows (length(y)) and 4 columns (length(x)), and each row is the corresponding y element repeated length(x) times. 4,4,4,4 3,3,3,3 9,9,9,9 Thanks. Fernando Álvarez __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Efficient way to do a merge in R
Any comments are very welcome, So I give it a shot, although I don't have answers but only some ideas which avenues I would explore, not being an expert at all: 1. I would try to be more restrictive with the columns used for merge, trying something like m1 - merge( x, y, by.x = V1, by.y = V1, all = TRUE ) 2. It may be an option to use match() directly: indices - match( y$V1, x$V1 ) That should give you a vector of 300,000 indices mapping the y values to their corresponding x records. I assume that there is always one record in y matching one record in x. You would still need to write some code to add the corresponding y values to a new column in x. 3. If that fails, and nobody else has a better idea, I would consider using a database engine for the job. Again, no expert advice, just a few ideas! Rgds, Rainer On Tuesday 04 October 2011 01:01:45 Aurélien PHILIPPOT wrote: Dear all, I am new in R and I have been faced with the following problem, that slows me down a lot. I am short of ideas to circumvent it. So, any help would be highly appreciated: I have 2 dataframes x and y. x is very big (70 million observations), whereas y is smaller (30 observations). All the observations of y are present in x. But y has one additional variable that I would like to incorporate to the dataframe x. For instance, imagine they have the following variable names: colnames(x)- c(V1, V2, V3, V4) and colnames(y)- c(V1, V2, V5) -Since the observations of y are present in x, my strategy was to merge x and y so that the dataframe x would get the values of the variable V5 for the observations that are both in x and y. -So, I did the following: dat- merge(x, y, all=TRUE). On a small example, it works fine. The only problem is that when I apply it to my big dataframe x, it really take for ever (several days and not done yet) and I have a very fast computer. So, I don't know whether I should stop now or keep on waiting. Does anyone have any idea to perform this operation in a more efficient way (in terms of computation time)? In addition, does anyone know how to incoporate some sort of counter in a program to check what how much work has been done at a given point of time? Any comments are very welcome, Thanks, Best, Aurelien [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] download files using ftp: avoid error
I haven't tested it thoroughly but what worked here is replacing download.file(url, destfile, quiet = FALSE) with sys_call - paste( wget, url, , destfile, sep= ) system( sys_call ) Program execution continues, whether or not the download from url was successful. However, wget is, I believe, not available on Windows. Rgds, Rainer On Friday 16 September 2011 15:07:15 Mary Kindall wrote: I am planning to download a large number of files from some website. I am using the following script. files2down = c('aaa', 'bbb', ) for (i in 1: len) { print(paste('downloading file', i, ' of total ', len)); url = paste(urlPrefix, files2down[i], sep='') destfile = paste (dest, 'inDir', files2down[i], sep='/' ) download.file(url, destfile, quiet = FALSE) } It works fine as long as the file is present. When the file is not present, it exit from loop. Is there a way to continue looping if error occurs. Thanks __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Reading large, non-tabular files
That looks like a perfect job for (g)awk which is in every Linux distribution but also available for Windows. It can be called with something like system( awk -f script.awk inputfile.txt ) and does its job silently and very fast. 650MB should not be an issue. I'm not proficient in awk but would offer my help anyway (off-list...). Rgds, Rainer On Wednesday 14 September 2011 13:08:14 Stefan McKinnon Høj-Edwards wrote: Dear R-help, I have a very large ascii data file, of which I only want to read in selected lines (e.g. on fourth of the lines); determining which lines depends on the lines content. So far, I have found two approaches for doing this in R; 1) Read the file line by line using a repeat-loop and save the result in a temporary file or a variable, and 2) Read the entire file and filter/reshape it using *apply methods. To my understanding, the use of repeat{}-loops are quite slow in R, and reading an entire file to discard 3 quarters of the data is a bit of an overkill. Not to mention loading an 650MB text file into memory. What I am looking for is a function, that works like the first approach, but avoiding do- or repeat-loops, so I imagine it is implemented in a lower-level language, to be more efficient. Naturally, when calling the function, one would provide a function that determines if/how the line should be appended to a variable. Alternatively, an object working as an generator (in Python terms), could be used with the normal *apply functions. I imagine this working differently from e.g. sapply(readLines(myfile.txt), FUN=selector), in that readLines would be executed first, loading the entire file into memory and supplying it to sapply, whereas the generator-object only reads a line when sapply requests the next element. Are there options for this kind of operation? Kind regards, Stefan McKinnon Høj-Edwards Dept. of Genetics and Biotechnology PhD student Faculty of Agricultural Sciences stefan.hoj-edwa...@agrsci.dkAarhus University Tel.: +45 8999 1291 Blichers Allé 20, Postboks 50 Web: www.iysik.com DK-8830 Tjele Tel.: +45 8999 1900 Web: www.agrsci.au.dk __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Subset function
Does that help: x xin xout 1 1 14 2 85 3 16 884 4 1 14 5 85 6 16 884 subset( x, x$xin 7, select = xout ) xout 25 3 884 55 6 884 Rgds, Rainer On Friday 09 September 2011 04:38:44 stat.kk wrote: Hi, can anyone help me how to use 'subset' function on my data frame? I have created data frame 'data' with a few variables and with row names. Now I would like to subset rows with concrete row names. Using data[] I know how to do it. But I dont know how to formulate the subset condition: subset(data, subset = ?, select = c(var1, var2)) Thank you very much, stat.kk -- View this message in context: http://r.789695.n4.nabble.com/Subset-function-tp3801397p3801397.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Asking Favor For Remove element with Particular Value In Vector
Not sure whether I understand your question right but here is what I would do: # Sample data x - seq( 1, 100, by=6) x [1] 1 7 13 19 25 31 37 43 49 55 61 67 73 79 85 91 97 # remove element with value 19 x - x[ x != 19 ] x [1] 1 7 13 25 31 37 43 49 55 61 67 73 79 85 91 97 If you want to remove values smaller / larger than a certain threshold, your way should work well: # Sample data x - seq( 1, 100, by=6) x[9] - 155 x [1] 1 7 13 19 25 31 37 43 155 55 61 67 73 79 85 91 97 # Remove elements smaller than 20 or larger than 80: x - x[ x 20 x 80 ] x [1] 25 31 37 43 55 61 67 73 79 So there is probably an issue with your data vector - why don't you dput() it? Rgds, Rainer On Saturday 27 August 2011 02:31:29 chuan_zl wrote: Dear All. I am Chuan. I am beginner for R.I facing some problem in remove element from vector.I have a vector with size 238 element as follow(a part) [1] 0 18 24 33 44..[238] 255 Let the vector label as x,I want remove element 0 and 255.I try use such function: x[x0 x255] However, I am fail since same results are give even try it for many times.I also try with shorter vector with 10 element. It is successfully resulted. So,want can I do for it. Kindly asking favor for expert here. Thank you very much. Chuan -- View this message in context: http://r.789695.n4.nabble.com/Asking-Favor-For-Remove-element-with-Particul ar-Value-In-Vector-tp3772779p3772779.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] To import data to R from Excel
In Excel, make sure that your data has a format that corresponds to an R data frame (first row with column names, consistent column size and data type). Export your .XLS worksheet as .CSV file (mydata.csv). In R, read it into a data frame with read.csv( mydata.csv ) From here, you shold be able to reshapte the data in a way that you can draw your boxplot. Rgds, Rainer On Thursday 11 August 2011 11:29:23 Blessy chemmannur Francis wrote: Sir, I am a beginner in R language. I want to import data from excel to R. My data contains not only numeric values but also string values.I want to draw a boxplot graph . I can't import or write this string data. it is essential for my graph.Please give me a suggestion for This. Thanking You, BLESSY.C.F. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Getting Started
cran.r-project.org/doc/manuals/R-intro.pdf cran.r-project.org/doc/contrib/Paradis-rdebuts_en.pdf cran.r-project.org/doc/contrib/Verzani-SimpleR.pdf cran.r-project.org/doc/contrib/Lemon-kickstart/kr_intro.html https://stat.ethz.ch/mailman/listinfo/r-help On Thursday 21 July 2011 10:46:43 Varsha Agrawal wrote: I am a new user and want to learn R from the most basic level. Suggest me a reading or a link. Thanks Varsha P.S. I would appreciate if you could also send me the link where I can read questions and answers posted by others. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] for/if loop in R
For me, this works: Now, I want to add a 4th column, trend pricedata$trend - 0 which can have 2 values 0 or 1. if return1%, trend=1 else trend=0. pricedata$trend - ifelse( pricedata$return .01, 1, 0 ) Rgds, Rainer On Thursday 21 July 2011 19:39:15 financial engineer wrote: hi, Can someone please help me figure out where I am making a mistake in my for/if loop: I have a data frame (112 rows) called pricedata with 3 columns: date, prices, return. Now, I want to add a 4th column, trend, which can have 2 values 0 or 1. if return1%, trend=1 else trend=0. so, this is what I did: trend-numeric(nrow(pricedata)) cbind(pricedata,trend) for(i in 2:nrow(pricedata)){ +if (return[i]0.01) trend[i]=1 else trend[i]=0 +} and it doesn't change the values in trend, despite the fact that the return column has several rows with values 0.01 -why? thx! [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] read.csv help
Can you explain a little more? I have created a small CSV file following your pattern which looks like this in a text editor: A,B,C,D,E 65,68,71,74,77 67,71,75,79,83 69,73,77,81,85 71,77,83,89,95 When I load it into R with x - read.csv( a.csv ) I get this which I think is what you would expect: x A B C D E 1 65 68 71 74 77 2 67 71 75 79 83 3 69 73 77 81 85 4 71 77 83 89 95 with rownames( x ) [1] 1 2 3 4 Here is the dput version: dput( x ) structure(list(A = c(65L, 67L, 69L, 71L), B = c(68L, 71L, 73L, 77L), C = c(71L, 75L, 77L, 83L), D = c(74L, 79L, 81L, 89L), E = c(77L, 83L, 85L, 95L)), .Names = c(A, B, C, D, E), class = data.frame, row.names = c(NA, -4L)) What is different with your data? Rgds, Rainer Original-Nachricht Datum: Tue, 19 Jul 2011 00:05:30 -0700 (PDT) Von: psombe srinivas.es...@gmail.com An: r-help@r-project.org Betreff: [R] read.csv help Hi, I'm a new R user and I'm having trouble with the read.csv command. It somehow treats the first column as a row name field even though it's not a row name. there are no missing columns/entries and i'm not sure how to resolve this. the format of my data is A, B, C, D,..(3984 columns) 12, 13, 41,..(all numeric) it either treats column A as rownames or if I explicitly disable row names with row.names = NULL field it right shifts all the columns like rowno. A B C Last column 1 12 13 41 NA Srinivas -- View this message in context: http://r.789695.n4.nabble.com/read-csv-help-tp3677454p3677454.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. -- --- Windows: Just say No. __ 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] as.numeric
It may be helpful to make sure that, in the dialog that pops up when saving a spreadsheet to CSV, the option Save cell content as shown is checked - that would leave numbers as numbers, not wrapping them in . That has helped me at least in a similar situation! Rgds, Rainer On Tuesday 12 July 2011 06:09:18 Sarah Goslee wrote: Jessica, This would be easier to solve if you gave us more information, like str(PE). However, my guess is that your data somewhere has a nonnumeric value in that column, so the entire column is being imported as factor. It's not really awful - R is converting those factor values to their numeric levels, just as you asked. The best solution is to find and deal with the nonnumeric value before you import your data (something else you did not tell us about). Failing that, you may find this useful: as.numeric(as.character(PE[1, 90:99])) Sarah On Tue, Jul 12, 2011 at 4:38 AM, Jessica Lam ma_lk...@yahoo.com.hk wrote: Dear R user, After I imported data (csv format) in R, I called it out. But it is in non-numeric format. Then using as.numeric function. However, the output is really awful ! PE[1,90:99] V90 V91 V92 V93 V94 V95 V96 V97 V98 V99 1 16.8467742 17.5853166 19.7400328 21.7277241 21.5015489 19.1922102 20.3351524 18.1615471 18.5479946 16.8983887 as.numeric(PE[1,90:99]) [1] 11 10 11 10 11 9 10 9 9 8 How can I solve the above problem?? Thanks so much! Jessica -- __ 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 many times occurs
On Saturday 02 July 2011 21:40:24 Trying To learn again wrote: Clumsy but it works (replace the bingo stuff with what you want to do next): x - as.matrix( read.table( input.txt) ) xdim - dim( x ) ix - xdim[ 1 ] jx - xdim[ 2 ] - 2 bingo - 0 for( i in 1:ix ) { for( j in 1:jx ) { if( x[i,j] == 8 x[i,j+1] == 9 x[i,j+2] == 2 ) { bingo - bingo + 1 } } } print( bingo ) I'm sure there are more elegant and efficient solutions! Rgds, Rainer Here the matrix as dput( x ): structure(c(8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 2L, 2L, 2L, 2L, 2L, 2L, 5L, 8L, 5L, 5L, 8L, 5L, 4L, 9L, 4L, 4L, 9L, 4L, 5L, 2L, 5L, 5L, 2L, 5L, 8L, 8L, 8L, 8L, 8L, 8L, 5L, 9L, 5L, 5L, 9L, 9L, 6L, 2L, 6L, 6L, 2L, 2L, 6L, 1L, 4L, 6L, 1L, 2L), .Dim = c(6L, 10L), .Dimnames = list(NULL, c(V1, V2, V3, V4, V5, V6, V7, V8, V9, V10))) __ 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 many times occurs
Sorry, I forgot to attach the original post, so here once more with a cosmetic change: x - as.matrix( read.table( matr.txt) ) bingo - 0 for( i in 1:dim( x )[1] ) { for( j in 1:dim( x )[2] - 2 ) { if( x[i,j] == 8 x[i,j+1] == 9 x[i,j+2] == 2 ) { bingo - bingo + 1 } } } print( bingo ) Rgds, Rainer On Saturday 02 July 2011 21:40:24 Trying To learn again wrote: Hi all, I have a data matrix likein input.txt 8 9 2 5 4 5 8 5 6 6 8 9 2 8 9 2 8 9 2 1 8 9 2 5 4 5 8 5 6 4 8 9 2 5 4 5 8 5 6 6 8 9 2 8 9 2 8 9 2 1 8 9 2 5 4 5 8 9 2 2 In this example will be an 6x10 matrix (or data frame) I want to detect how many times in a row appears this combination 8 follewd by 9 followed by 2, and create a new matrix with only this number of occurs then if this number is less than n I keep the row. For example in the last row the number n will be 2 because series 8 9 2 appears 2 times in the same row. I tried this, but doesn´t worksalso tried other thinks but also the same results: *dat-read.table('input1.txt')* ** ** *dat1 - dat[ dat[,1]=8 dat[,2]=9 dat[,3]=2 ,]=1* *dat2-dat[(dat[,2]= 8 dat[,3]=9 dat[,4]=2),]=1* *dat3-dat[(dat[,5]=8dat[,4]=9 dat[,5]=2),]=1* *dat4-dat[(dat[,4]=8dat[,5]=9 dat[,6]=2),]=1* *dat5-dat[(dat[,5]=8dat[,6]=9 dat[,7]=2),]=1* *dat6-dat[(dat[,6]=8dat[,7]=9 dat[,8]=2),6]=1* *dat7-dat[(dat[,7]=8 dat[,8]=9 dat[,9]=2),7]=1* *dat8-dat[(dat[,8]=8 dat[,9]=9 dat[,10]=2),8]=1* ** datfinal-dat1+da2+dat3+dat4+dat5+dat6+dat7+dat8 final2 - dat[ rowSums(datfinal) 2 , ] So my last matrix final2 will be dat without the rows that doesn´t pass the conditions. [[alternative HTML version deleted]] dput( x ): structure(c(8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 2L, 2L, 2L, 2L, 2L, 2L, 5L, 8L, 5L, 5L, 8L, 5L, 4L, 9L, 4L, 4L, 9L, 4L, 5L, 2L, 5L, 5L, 2L, 5L, 8L, 8L, 8L, 8L, 8L, 8L, 5L, 9L, 5L, 5L, 9L, 9L, 6L, 2L, 6L, 6L, 2L, 2L, 6L, 1L, 4L, 6L, 1L, 2L), .Dim = c(6L, 10L), .Dimnames = list(NULL, c(V1, V2, V3, V4, V5, V6, V7, V8, V9, V10))) __ 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 export to pdf in landscape orientation?
Tell PDF the size of your piece of paper - here is what works for me: pdf( file = result.pdf, width = 28, height = 18 ) # numbers are cm some( stuff ) dev.off() Hope it helps, Rainer On Saturday 25 June 2011 18:46:28 Juan Andres Hernandez wrote: Does anybody know how to get a pdf file with landscape orientation?. pdf(file= 'my_file.pdf' ,onefile=T,paper='A4') plot(sin, -pi, 2*pi) dev.off() Thank's in advance Juan A. Hernandez Spain [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Unable to require installed package
On Thursday, January 27, 2011 08:57:01 am 刘力平 wrote: who sincerely thinks R is not google friendly, since R is a awful keyword. http://www.rseek.org/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Find the sign
On Monday, January 24, 2011 07:18:03 pm Alaios wrote: Hello :) I wanted to right an expression to check when x and y have the same sign and I wrote the following: if ((x0 y0) || (x0 y0)) which looks pretty ugly to me. Can you please suggest me a better way for that? Regards Alex __ 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. if( sign( x ) == sign( y ) ) __ 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] Normal Distribution Quantiles
Altogether I got five more or less silly solutions (not my judgment!), some of them further discussed in private mail, for a problem where my expectation was to get a simple one-liner back: Check ?clt or so... Fortunately, with all of them I seem to arrive at a result that is consistent with what my expressions evaluates to (104.25) which gives me a great opportunity to play around with the various approaches - brain fodder for quite a few days. Great experience, Thanks to all, Rainer __ 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] Normal Distribution Quantiles
This is probably embarrassingly basic, but I have spent quite a few hours in Google and RSeek without getting a clue - probably I'm asking the wrong questions... There is this guy who has decided to walk through Australia, a total distance of 4000 km. His daily portion (mean) is 40km with an sd of 10 km. I want to calculate the number of days it takes to arrive with 80, 90, 95, 99% probability. I know how to do this manually, eg. for 95% $\Phi \left( \frac{4000-40n}{10 \sqrt{n}} \right) \leq 0.05$ find the z score... but how would I do this in R? Not qnorm(), but what is it? Thanks in advance, and apologies for the level of question... Rainer __ 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] Normal Distribution Quantiles
Sounds like homework, which is not an encouraged use of the Rhelp list. You can either do it in theory... It is _from_ a homework but I have the solution already (explicitly got that done first!) - this was the pasted Latex code (apologies for that, but in plain text it looks unreadable[1], and I thought everybody here has his / her favorite Latrex editor open all the time anyway...). I'm just looking, for my own advancement and programming training, for a way of doing that in R - which, from your and Dennis' reply, doesn't seem to exist. I would _not_ misuse the list for getting homework done easily, I will not ask learning statistics questions here, and I will always try to find the solution myself before posting something here, I promise! Thanks anyway for the simulation advice, Rainer (4000 - (40*n)) -329 [1] --- = 1200 (10*(n^-)) 2 On Saturday 08 January 2011 14:56:20 you wrote: On Jan 8, 2011, at 6:56 AM, Rainer Schuermann wrote: This is probably embarrassingly basic, but I have spent quite a few hours in Google and RSeek without getting a clue - probably I'm asking the wrong questions... There is this guy who has decided to walk through Australia, a total distance of 4000 km. His daily portion (mean) is 40km with an sd of 10 km. I want to calculate the number of days it takes to arrive with 80, 90, 95, 99% probability. I know how to do this manually, eg. for 95% $\Phi \left( \frac{4000-40n}{10 \sqrt{n}} \right) \leq 0.05$ find the z score... but how would I do this in R? Not qnorm(), but what is it? Sounds like homework, which is not an encouraged use of the Rhelp list. You can either do it in theory or you can simulate it. Here's a small step toward a simulation approach. cumsum(rnorm(100, mean=40, sd=10)) [1] 41.90617 71.09148 120.55569 159.56063 229.73167 255.35290 300.74655 snipped [92] 3627.25753 3683.24696 3714.11421 3729.41203 3764.54192 3809.15159 3881.71016 [99] 3917.16512 3932.00861 cumsum(rnorm(100, mean=40, sd=10)) [1] 38.59288 53.82815 111.30052 156.58190 188.15454 207.90584 240.64078 snipped [92] 3776.25476 3821.90626 3876.64512 3921.16797 3958.83472 3992.33155 4045.96649 [99] 4091.66277 4134.45867 The first realization did not make it in the expected 100 days so further efforts should extend the simulation runs to maybe 120 days. The second realization had him making it on the 98th day. There is an R replicate() function available once you get a function running that will return a specific value for an instance. This one might work: min(which(cumsum(rnorm(120, mean=40, sd=10)) = 4000) ) [1] 97 If you wanted a forum that does not explicitly discourage homework and would be a better place to ask theory and probability questions, there is CrossValidated: http://stats.stackexchange.com/faq Thanks in advance, and apologies for the level of question... Rainer __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. David Winsemius, MD West Hartford, CT __ 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] Numbers in a string
If your OS is Linux, you might want to look at sed or gawk. They are very good and efficient for such tasks. You need it once or as a part of program? Some samples would be helpful... Rgds, Rainer Original-Nachricht Datum: Wed, 15 Dec 2010 16:55:26 +0800 Von: Luis Felipe Parra felipe.pa...@quantil.com.co An: r-help r-help@r-project.org Betreff: [R] Numbers in a string Hello, I have stings which have all sort of characters (numbers, letters, punctuation marks, etc) I would like to stay only with the numbers in them, does somebody know how to do this? Thank you Felipe Parra [[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. -- --- Windows: Just say No. __ 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.