Re: [R] combine barchart and xyplot in lattice
I converted to factor because, in barchart, x-axis seems to be factor only. Without factor, x labels are 1, 2, 3, … Solution 1 works for me. If there is a method for barchart, I am interested in looking at that as well. Thanks, Naresh Sent from my iPhone > On Nov 18, 2023, at 10:09 AM, Deepayan Sarkar > wrote: > > On Sat, 18 Nov 2023 at 06:44, Naresh Gurbuxani > wrote: >> >> In below graph, I would like to add two vertical lines using >> panel.abline(). Is this possible? > > I assume you want the 'v' variable in panel.abline() to be interpreted > in the context of your x-axis, which here represents a factor > variable. Unless two factor variables have the same levels, their > values don't really mean the same thing. So either you need to specify > the levels, or make the axis numeric: > > # option 1 - factor > xyplot(count ~ as.factor(hour), type = "l", lwd = 2, > col = 2, data = vehicles, > panel = function(x, y, ...) { > panel.xyplot(x, y, ...) > panel.abline(v = factor(c("9", "17"), levels = levels(x)), >lty = 2, col = "gray") > }) > > # option 2 - numeric > xyplot(count ~ hour, type = "l", lwd = 2, > col = 2, data = vehicles, > panel = function(x, y, ...) { > panel.xyplot(x, y, ...) > panel.abline(v = c(9, 17), lty = 2, col = "gray") > }) > > Best, > -Deepayan > >> >> Thanks, >> Naresh >> >> mydf <- data.frame(hour = rep(6:20, 2), >> traffic = c(round(dnorm(6:20, 9, 3) * 1), round(dnorm(6:20, 17, 4) * >> 1)), >> direction = rep(c("inbound", "outbound"), c(15, 15))) >> >> vehicles <- data.frame(hour = 6:20, >> count = c(100, 120, 140, 125, 105, 80, 70, 75, 80, 100, 110, 120, 115, >> 110, 100)) >> >> library(lattice) >> library(latticeExtra) >> >> # This works >> mybars <- barchart(traffic ~ as.factor(hour), groups = direction, >> stack = TRUE, horizontal = FALSE, data = mydf, >> auto.key = list(columns = 2, space = "bottom"), xlab = "hour") >> >> mylines <- xyplot(count ~ as.factor(hour), type = "l", lwd = 2, >> col = 2, data = vehicles) >> >> mybars + mylines >> >> # This does not work. Lines are not correctly placed. >> mylines <- xyplot(count ~ as.factor(hour), type = "l", lwd = 2, >> col = 2, data = vehicles, panel = function(x, y, ...) { >> panel.xyplot(x, y, ...) >> panel.abliine(v = as.factor(c(9, 17)), lty = 2, col = "gray") >> }) >> __ >> 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] combine barchart and xyplot in lattice
On Sat, 18 Nov 2023 at 06:44, Naresh Gurbuxani wrote: > > In below graph, I would like to add two vertical lines using > panel.abline(). Is this possible? I assume you want the 'v' variable in panel.abline() to be interpreted in the context of your x-axis, which here represents a factor variable. Unless two factor variables have the same levels, their values don't really mean the same thing. So either you need to specify the levels, or make the axis numeric: # option 1 - factor xyplot(count ~ as.factor(hour), type = "l", lwd = 2, col = 2, data = vehicles, panel = function(x, y, ...) { panel.xyplot(x, y, ...) panel.abline(v = factor(c("9", "17"), levels = levels(x)), lty = 2, col = "gray") }) # option 2 - numeric xyplot(count ~ hour, type = "l", lwd = 2, col = 2, data = vehicles, panel = function(x, y, ...) { panel.xyplot(x, y, ...) panel.abline(v = c(9, 17), lty = 2, col = "gray") }) Best, -Deepayan > > Thanks, > Naresh > > mydf <- data.frame(hour = rep(6:20, 2), > traffic = c(round(dnorm(6:20, 9, 3) * 1), round(dnorm(6:20, 17, 4) * > 1)), > direction = rep(c("inbound", "outbound"), c(15, 15))) > > vehicles <- data.frame(hour = 6:20, > count = c(100, 120, 140, 125, 105, 80, 70, 75, 80, 100, 110, 120, 115, > 110, 100)) > > library(lattice) > library(latticeExtra) > > # This works > mybars <- barchart(traffic ~ as.factor(hour), groups = direction, > stack = TRUE, horizontal = FALSE, data = mydf, > auto.key = list(columns = 2, space = "bottom"), xlab = "hour") > > mylines <- xyplot(count ~ as.factor(hour), type = "l", lwd = 2, > col = 2, data = vehicles) > > mybars + mylines > > # This does not work. Lines are not correctly placed. > mylines <- xyplot(count ~ as.factor(hour), type = "l", lwd = 2, > col = 2, data = vehicles, panel = function(x, y, ...) { > panel.xyplot(x, y, ...) > panel.abliine(v = as.factor(c(9, 17)), lty = 2, col = "gray") > }) > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] combine barchart and xyplot in lattice
In below graph, I would like to add two vertical lines using panel.abline(). Is this possible? Thanks, Naresh mydf <- data.frame(hour = rep(6:20, 2), traffic = c(round(dnorm(6:20, 9, 3) * 1), round(dnorm(6:20, 17, 4) * 1)), direction = rep(c("inbound", "outbound"), c(15, 15))) vehicles <- data.frame(hour = 6:20, count = c(100, 120, 140, 125, 105, 80, 70, 75, 80, 100, 110, 120, 115, 110, 100)) library(lattice) library(latticeExtra) # This works mybars <- barchart(traffic ~ as.factor(hour), groups = direction, stack = TRUE, horizontal = FALSE, data = mydf, auto.key = list(columns = 2, space = "bottom"), xlab = "hour") mylines <- xyplot(count ~ as.factor(hour), type = "l", lwd = 2, col = 2, data = vehicles) mybars + mylines # This does not work. Lines are not correctly placed. mylines <- xyplot(count ~ as.factor(hour), type = "l", lwd = 2, col = 2, data = vehicles, panel = function(x, y, ...) { panel.xyplot(x, y, ...) panel.abliine(v = as.factor(c(9, 17)), lty = 2, col = "gray") }) __ 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] Combine two dataframe with different row number and interpolation between values
Hallo And missing value interpolation is rather tricky business dependent on what is underlying process. Maybe na.locf from zoo package? Or approxfun?, splinefun? Cheers Petr > -Original Message- > From: R-help On Behalf Of javad bayat > Sent: Wednesday, August 31, 2022 8:09 AM > To: r-help@r-project.org > Subject: [R] Combine two dataframe with different row number and > interpolation between values > > Dear all, > I am trying to combine two large dataframe in order to make a dataframe with > exactly the dimension of the second dataframe. > The first df is as follows: > > df1 = data.frame(y = rep(c(2010,2011,2012,2013,2014), each = 2920), d = > rep(c(1:365,1:365,1:365,1:365,1:365),each=8), > h = rep(c(seq(3,24, by = 3),seq(3,24, by = 3),seq(3,24, by = 3),seq(3,24, by = > 3),seq(3,24, by = 3)),365), > ws = rnorm(1:14600, mean=20)) > > head(df1) > y d hws > 1 2010 1 3 20.71488 > 2 2010 1 6 19.70125 > 3 2010 1 9 21.00180 > 4 2010 1 12 20.29236 > 5 2010 1 15 20.12317 > 6 2010 1 18 19.47782 > > The data in the "ws" column were measured with 3 hours frequency and I need > data with one hour frequency. I have made a second df as follows with one hour > frequency for the "ws" column. > > df2 = data.frame(y = rep(c(2010,2011,2012,2013,2014), each = 8760), d = > rep(c(1:365,1:365,1:365,1:365,1:365),each=24), > h = rep(c(1:24,1:24,1:24,1:24,1:24),365), ws = "NA") > > head(df2) > y dh ws > 1 2010 11 NA > 2 2010 12 NA > 3 2010 13 NA > 4 2010 14 NA > 5 2010 15 NA > 6 2010 16 NA > > What I am trying to do is combine these two dataframes so as to the rows in > df1 (based on the values of "y", "d", "h" columns) that have values exactly > similar to df2's rows copied in its place in the new df (df3). > For example, in the first dataframe the first row was measured at 3 o'clock on > the first day of 2010 and this row must be placed on the third row of the second > dataframe which has a similar value (2010, 1, 3). Like the below > table: > y dh ws > 1 2010 11 NA > 2 2010 12 NA > 3 2010 13 20.71488 > 4 2010 14 NA > 5 2010 15 NA > 6 2010 16 19.70125 > > But regarding the values of the "ws" column for df2 that do not have value (at 4 > and 5 o'clock), I need to interpolate between the before and after values to fill in > the missing data of the "ws". > I have tried the following codes but they did not work correctly. > > > df3 = merge(df1, df2, by = "y") > Error: cannot allocate vector of size 487.9 Mb or > > library(dplyr) > > df3<- df1%>% full_join(df2) > > > Is there any way to do this? > Sincerely > > > > > > -- > Best Regards > Javad Bayat > M.Sc. Environment Engineering > Alternative Mail: bayat...@yahoo.com > > [[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] Combine two dataframe with different row number and interpolation between values
Hallo Merge df3 <- merge(df1, df2, all=T) > head(df3) y d h ws 1 2010 1 1 NA 2 2010 1 2 NA 3 2010 1 3 20.4631367884005 4 2010 1 3 NA 5 2010 1 4 NA 6 2010 1 5 NA Cheers Petr > -Original Message- > From: R-help On Behalf Of javad bayat > Sent: Wednesday, August 31, 2022 8:09 AM > To: r-help@r-project.org > Subject: [R] Combine two dataframe with different row number and > interpolation between values > > Dear all, > I am trying to combine two large dataframe in order to make a dataframe > with exactly the dimension of the second dataframe. > The first df is as follows: > > df1 = data.frame(y = rep(c(2010,2011,2012,2013,2014), each = 2920), d = > rep(c(1:365,1:365,1:365,1:365,1:365),each=8), > h = rep(c(seq(3,24, by = 3),seq(3,24, by = 3),seq(3,24, by = > 3),seq(3,24, by = 3),seq(3,24, by = 3)),365), > ws = rnorm(1:14600, mean=20)) > > head(df1) > y d hws > 1 2010 1 3 20.71488 > 2 2010 1 6 19.70125 > 3 2010 1 9 21.00180 > 4 2010 1 12 20.29236 > 5 2010 1 15 20.12317 > 6 2010 1 18 19.47782 > > The data in the "ws" column were measured with 3 hours frequency and I need > data with one hour frequency. I have made a second df as follows with one > hour frequency for the "ws" column. > > df2 = data.frame(y = rep(c(2010,2011,2012,2013,2014), each = 8760), d = > rep(c(1:365,1:365,1:365,1:365,1:365),each=24), > h = rep(c(1:24,1:24,1:24,1:24,1:24),365), ws = "NA") > > head(df2) > y dh ws > 1 2010 11 NA > 2 2010 12 NA > 3 2010 13 NA > 4 2010 14 NA > 5 2010 15 NA > 6 2010 16 NA > > What I am trying to do is combine these two dataframes so as to the rows in > df1 (based on the values of "y", "d", "h" columns) that have values exactly > similar to df2's rows copied in its place in the new df (df3). > For example, in the first dataframe the first row was measured at 3 o'clock > on the first day of 2010 and this row must be placed on the third row of > the second dataframe which has a similar value (2010, 1, 3). Like the below > table: > y dh ws > 1 2010 11 NA > 2 2010 12 NA > 3 2010 13 20.71488 > 4 2010 14 NA > 5 2010 15 NA > 6 2010 16 19.70125 > > But regarding the values of the "ws" column for df2 that do not have value > (at 4 and 5 o'clock), I need to interpolate between the before and after > values to fill in the missing data of the "ws". > I have tried the following codes but they did not work correctly. > > > df3 = merge(df1, df2, by = "y") > Error: cannot allocate vector of size 487.9 Mb > or > > library(dplyr) > > df3<- df1%>% full_join(df2) > > > Is there any way to do this? > Sincerely > > > > > > -- > Best Regards > Javad Bayat > M.Sc. Environment Engineering > Alternative Mail: bayat...@yahoo.com > > [[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] combine filter() and select()
On Wed, Aug 19, 2020 at 10:03 AM Ivan Calandra wrote: > > Dear useRs, > > I'm new to the tidyverse world and I need some help on basic things. > > I have the following tibble: > mytbl <- structure(list(files = c("a", "b", "c", "d", "e", "f"), prop = > 1:6), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame")) > > I want to subset the rows with "a" in the column "files", and keep only > that column. > > So I did: > myfile <- mytbl %>% > filter(grepl("a", files)) %>% > select(files) > > It works, but I believe there must be an easier way to combine filter() > and select(), right? Not in the tidyverse. As others have mentioned, both [ and subset() in base R allow you to simultaneously subset rows and columns, but there's no single verb in the tidyverse that does both. This is somewhat informed by the observation that in data frames, unlike matrices, rows and columns are not exchangeable, and you typically want to express subsetting in rather different ways. Hadley -- http://hadley.nz __ 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] combine filter() and select()
A kind of hybrid answer is to use base::subset(), which supports non-standard evaluation (it searches for unquoted symbols like 'files' in the code line below in the object that is its first argument; %>% puts 'mytbl' in that first position) and row (filter) and column (select) subsets > mytbl %>% subset(files %in% "a", files) # A tibble: 1 x 1 files 1 a Or subset(grepl("a", files), files) if that was what you meant. One important idea that the tidyverse implements is, in my opinion, 'endomorphism' -- you get back the same type of object as you put in -- so I wouldn't use a base R idiom that returned a vector unless that were somehow essential for the next step in the analysis. There is value in having separate functions for filter() and select(), and probably there are edge cases where filter(), select(), and subset() behave differently, but for what it's worth subset() can be used to perform these operations individually > mytbl %>% subset(, files) # A tibble: 6 x 1 files 1 a 2 b 3 c 4 d 5 e 6 f > mytbl %>% subset(grepl("a", files), ) # A tibble: 1 x 2 files prop 1 a 1 Martin Morgan On 8/20/20, 2:48 AM, "R-help on behalf of Ivan Calandra" wrote: Hi Jeff, The code you show is exactly what I usually do, in base R; but I wanted to play with tidyverse to learn it (and also understand when it makes sense and when it doesn't). And yes, of course, in the example I gave, I end up with a 1-cell tibble, which could be better extracted as a length-1 vector. But my real goal is not to end up with a single value or even a single column. I just thought that simplifying my example was the best approach to ask for advice. But thank you for letting me know that what I'm doing is pointless! Ivan -- Dr. Ivan Calandra TraCEr, laboratory for Traceology and Controlled Experiments MONREPOS Archaeological Research Centre and Museum for Human Behavioural Evolution Schloss Monrepos 56567 Neuwied, Germany +49 (0) 2631 9772-243 https://www.researchgate.net/profile/Ivan_Calandra On 19/08/2020 19:27, Jeff Newmiller wrote: > The whole point of dplyr primitives is to support data frames... that is, lists of columns. When you pare your data frame down to one column you are almost certainly using the wrong tool for the job. > > So, sure, your code works... and it even does what you wanted in the dplyr style, but what a pointless exercise. > > grep( "a", mytbl$file, value=TRUE ) > > On August 19, 2020 7:56:32 AM PDT, Ivan Calandra wrote: >> Dear useRs, >> >> I'm new to the tidyverse world and I need some help on basic things. >> >> I have the following tibble: >> mytbl <- structure(list(files = c("a", "b", "c", "d", "e", "f"), prop = >> 1:6), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame")) >> >> I want to subset the rows with "a" in the column "files", and keep only >> that column. >> >> So I did: >> myfile <- mytbl %>% >> filter(grepl("a", files)) %>% >> select(files) >> >> It works, but I believe there must be an easier way to combine filter() >> and select(), right? >> >> Thank you! >> Ivan __ 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] combine filter() and select()
Hi Jeff, The code you show is exactly what I usually do, in base R; but I wanted to play with tidyverse to learn it (and also understand when it makes sense and when it doesn't). And yes, of course, in the example I gave, I end up with a 1-cell tibble, which could be better extracted as a length-1 vector. But my real goal is not to end up with a single value or even a single column. I just thought that simplifying my example was the best approach to ask for advice. But thank you for letting me know that what I'm doing is pointless! Ivan -- Dr. Ivan Calandra TraCEr, laboratory for Traceology and Controlled Experiments MONREPOS Archaeological Research Centre and Museum for Human Behavioural Evolution Schloss Monrepos 56567 Neuwied, Germany +49 (0) 2631 9772-243 https://www.researchgate.net/profile/Ivan_Calandra On 19/08/2020 19:27, Jeff Newmiller wrote: > The whole point of dplyr primitives is to support data frames... that is, > lists of columns. When you pare your data frame down to one column you are > almost certainly using the wrong tool for the job. > > So, sure, your code works... and it even does what you wanted in the dplyr > style, but what a pointless exercise. > > grep( "a", mytbl$file, value=TRUE ) > > On August 19, 2020 7:56:32 AM PDT, Ivan Calandra wrote: >> Dear useRs, >> >> I'm new to the tidyverse world and I need some help on basic things. >> >> I have the following tibble: >> mytbl <- structure(list(files = c("a", "b", "c", "d", "e", "f"), prop = >> 1:6), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame")) >> >> I want to subset the rows with "a" in the column "files", and keep only >> that column. >> >> So I did: >> myfile <- mytbl %>% >> filter(grepl("a", files)) %>% >> select(files) >> >> It works, but I believe there must be an easier way to combine filter() >> and select(), right? >> >> Thank you! >> Ivan __ 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] combine filter() and select()
Dear Chris, I didn't think about having the assignment at the end as you showed; it indeed fits the pipe workflow better. By "easy", I actually meant shorter. As you said, in base R, I usually do that in 1 line, so I was hoping to do the same in tidyverse. But I'm glad to hear that I'm using tidyverse the proper way :) Best regards, Ivan -- Dr. Ivan Calandra TraCEr, laboratory for Traceology and Controlled Experiments MONREPOS Archaeological Research Centre and Museum for Human Behavioural Evolution Schloss Monrepos 56567 Neuwied, Germany +49 (0) 2631 9772-243 https://www.researchgate.net/profile/Ivan_Calandra On 19/08/2020 19:21, Chris Evans wrote: > Inline > > - Original Message - >> From: "Ivan Calandra" >> To: "R-help" >> Sent: Wednesday, 19 August, 2020 16:56:32 >> Subject: [R] combine filter() and select() >> Dear useRs, >> >> I'm new to the tidyverse world and I need some help on basic things. >> >> I have the following tibble: >> mytbl <- structure(list(files = c("a", "b", "c", "d", "e", "f"), prop = >> 1:6), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame")) >> >> I want to subset the rows with "a" in the column "files", and keep only >> that column. >> >> So I did: >> myfile <- mytbl %>% >> filter(grepl("a", files)) %>% >> select(files) >> >> It works, but I believe there must be an easier way to combine filter() >> and select(), right? > I would write > > mytbl %>% > filter(grepl("a", files)) %>% > select(files) -> myfile > > as I like to keep a sort of "top to bottom and left to right" flow when > writing in the tidyverse dialect of R but that's really not important. > > Apart from that I think what you've done is "proper tidyverse". To me another > difference between the dialects is that classical R often seems to put value > on, and make it easy, to do things with incredible few characters. I think > the people who are brilliant at that sort of coding, and there are many on > this list, that sort of coding is also easy to read. I know that Chinese is > easy to read if you grew up on it but to a bear of little brain like me, the > much more verbose style of tidyverse repays typing time with readability when > I come back to my code and, though I have little experience of this yet, when > I read other poeple's code. > > What did you think wasn't "easy" about what you wrote? > > Very best (all), > > Chris > >> Thank you! >> Ivan >> >> -- >> Dr. Ivan Calandra >> TraCEr, laboratory for Traceology and Controlled Experiments >> MONREPOS Archaeological Research Centre and >> Museum for Human Behavioural Evolution >> Schloss Monrepos >> 56567 Neuwied, Germany >> +49 (0) 2631 9772-243 >> https://www.researchgate.net/profile/Ivan_Calandra >> >> __ >> 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] combine filter() and select()
The whole point of dplyr primitives is to support data frames... that is, lists of columns. When you pare your data frame down to one column you are almost certainly using the wrong tool for the job. So, sure, your code works... and it even does what you wanted in the dplyr style, but what a pointless exercise. grep( "a", mytbl$file, value=TRUE ) On August 19, 2020 7:56:32 AM PDT, Ivan Calandra wrote: >Dear useRs, > >I'm new to the tidyverse world and I need some help on basic things. > >I have the following tibble: >mytbl <- structure(list(files = c("a", "b", "c", "d", "e", "f"), prop = >1:6), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame")) > >I want to subset the rows with "a" in the column "files", and keep only >that column. > >So I did: >myfile <- mytbl %>% > filter(grepl("a", files)) %>% > select(files) > >It works, but I believe there must be an easier way to combine filter() >and select(), right? > >Thank you! >Ivan -- Sent from my phone. Please excuse my brevity. __ 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] combine filter() and select()
Inline - Original Message - > From: "Ivan Calandra" > To: "R-help" > Sent: Wednesday, 19 August, 2020 16:56:32 > Subject: [R] combine filter() and select() > Dear useRs, > > I'm new to the tidyverse world and I need some help on basic things. > > I have the following tibble: > mytbl <- structure(list(files = c("a", "b", "c", "d", "e", "f"), prop = > 1:6), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame")) > > I want to subset the rows with "a" in the column "files", and keep only > that column. > > So I did: > myfile <- mytbl %>% > filter(grepl("a", files)) %>% > select(files) > > It works, but I believe there must be an easier way to combine filter() > and select(), right? I would write mytbl %>% filter(grepl("a", files)) %>% select(files) -> myfile as I like to keep a sort of "top to bottom and left to right" flow when writing in the tidyverse dialect of R but that's really not important. Apart from that I think what you've done is "proper tidyverse". To me another difference between the dialects is that classical R often seems to put value on, and make it easy, to do things with incredible few characters. I think the people who are brilliant at that sort of coding, and there are many on this list, that sort of coding is also easy to read. I know that Chinese is easy to read if you grew up on it but to a bear of little brain like me, the much more verbose style of tidyverse repays typing time with readability when I come back to my code and, though I have little experience of this yet, when I read other poeple's code. What did you think wasn't "easy" about what you wrote? Very best (all), Chris > > Thank you! > Ivan > > -- > Dr. Ivan Calandra > TraCEr, laboratory for Traceology and Controlled Experiments > MONREPOS Archaeological Research Centre and > Museum for Human Behavioural Evolution > Schloss Monrepos > 56567 Neuwied, Germany > +49 (0) 2631 9772-243 > https://www.researchgate.net/profile/Ivan_Calandra > > __ > 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. -- Small contribution in our coronavirus rigours: https://www.coresystemtrust.org.uk/home/free-options-to-replace-paper-core-forms-during-the-coronavirus-pandemic/ Chris Evans Visiting Professor, University of Sheffield I do some consultation work for the University of Roehampton and other places but remains my main Email address. I have a work web site at: https://www.psyctc.org/psyctc/ and a site I manage for CORE and CORE system trust at: http://www.coresystemtrust.org.uk/ I have "semigrated" to France, see: https://www.psyctc.org/pelerinage2016/semigrating-to-france/ https://www.psyctc.org/pelerinage2016/register-to-get-updates-from-pelerinage2016/ If you want an Emeeting, I am trying to keep them to Thursdays and my diary is at: https://www.psyctc.org/pelerinage2016/ceworkdiary/ Beware: French time, generally an hour ahead of UK. __ 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] combine filter() and select()
Dear useRs, I'm new to the tidyverse world and I need some help on basic things. I have the following tibble: mytbl <- structure(list(files = c("a", "b", "c", "d", "e", "f"), prop = 1:6), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame")) I want to subset the rows with "a" in the column "files", and keep only that column. So I did: myfile <- mytbl %>% filter(grepl("a", files)) %>% select(files) It works, but I believe there must be an easier way to combine filter() and select(), right? Thank you! Ivan -- Dr. Ivan Calandra TraCEr, laboratory for Traceology and Controlled Experiments MONREPOS Archaeological Research Centre and Museum for Human Behavioural Evolution Schloss Monrepos 56567 Neuwied, Germany +49 (0) 2631 9772-243 https://www.researchgate.net/profile/Ivan_Calandra __ 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] Combine recursive lists in a single list or data frame and write it to file
Thanks again Jim. The links below are for 2 files (papers) i downloaded from Google Scholar for testing. You can use either both or any other pdf files with tables. Thanks again-EK. https://pdfs.semanticscholar.org/50a4/2b8146f08161b1036457fe0d241b6b898974.pdf https://pdfs.semanticscholar.org/50a4/2b8146f08161b1036457fe0d241b6b898974.pdf The code: install.packages("rJava");library(rJava) install.packages("tabulizer");library(tabulizer) MyPath - "C:/Users/name/Documents/Temp" ExtTable - function (Path,CalOrd){ FileNames - dir(Path, pattern =".(pdf|PDF)",full.names = TRUE) MyFiles - lapply(FileNames, function(i) extract_tables(i,method = "stream")) if(CalOrd == "Yes"){ MyOFiles - gsub("(\\s.*)|(.pdf|.PDF)","",basename(FileNames)) MyOFiles - match(MyOFiles,month.name) MyNFiles - MyFiles[order(MyOFiles)]} else MyFiles } MyTables - ExtTable(Path=MyPath,CalOrd = "Yes") On Thu, Dec 20, 2018 at 12:28 AM Jim Lemon wrote: > > Hi Ek, > It looks to me as though you are not joining the lists into a single > list, then calling FillList and then converting to a data frame. If > you can send some data (if it's not too big) I can test it and make > sure that it works, as it did every time for me. > > Jim > > On Thu, Dec 20, 2018 at 2:22 PM Ek Esawi wrote: > > > > Thank you Jim. I did use unlist with the recursive option which > > converted the 3 levels list to a list of 38 matrices. I tried your > > earlier function to join the 38 matrices, all of which have different > > number of columns and rows, but i kept getting an error. > > > > fillList<-function(x) { > > + maxrows<-max(unlist(lapply(x,length))) > > + return(lapply(x,"[",1:maxrows)) > > + } > > > > > > for (i in 1:length(MyTables)) { > > + write.table(as.data.frame(fillList(MyTables[i])), > > + file = "Temp.txt",append = TRUE,quote = TRUE)} > > Error in (function (..., row.names = NULL, check.rows = FALSE, > > check.names = TRUE, : > > arguments imply differing number of rows: 3, 55, 56, 53, 54, 16, 21, > > 23, 50, 24 > > > > > > On Wed, Dec 19, 2018 at 9:36 PM Jim Lemon wrote: > > > > > > Hi Ek, > > > Look at unlist and the argument "recursive". You can step down through > > > the levels or a nested list to convert it to a single level list. > > > > > > Jim > > > > > > On Thu, Dec 20, 2018 at 1:33 PM Ek Esawi wrote: > > > > > > > > Thank you Bert. I don't see how unlist will help. I want to combine > > > > them but keep the "rectangular structure",e.g. list, data frame, > > > > matrix because i want to get the tables in their original form. > > > > Unlist converts the whole output to a single vector; unless i am > > > > missing something. > > > > > > > > On Wed, Dec 19, 2018 at 9:10 PM Bert Gunter > > > > wrote: > > > > > > > > > > Does ?unlist not help? Why not? > > > > > > > > > > Bert > > > > > > > > > > > > > > > On Wed, Dec 19, 2018, 5:13 PM Ek Esawi > > > >> > > > > >> Hi All— > > > > >> > > > > >> I am using the R tabulizer package to extract tables from pdf files. > > > > >> The output is a set of lists of matrices. The package extracts tables > > > > >> and a lot of extra stuff which is nearly impossible to clean with > > > > >> RegEx. So, I want to clean it manually. > > > > >> To do so I need to (1) combine all lists in a single list or data > > > > >> frame and (2) then write the single entity to a text file to edit it. > > > > >> I could not figure out how. > > > > >> > > > > >> I tried something like this but did not work. > > > > >> lapply(MyTables, function(x) > > > > >> lapply(x,write.table(file="temp.txt",append = TRUE))) > > > > >> > > > > >> Any help is greatly appreciated. > > > > >> > > > > >> Here is my code: > > > > >> > > > > >> install.packages("rJava");library(rJava) > > > > >> install.packages("tabulizer");library(tabulizer) > > > > >> MyPath <- "C:/Users/name/Documents/tEMP" > > > > >> ExtTable <- function (Path,CalOrd){ > > > > >> FileNames <- dir(Path, pattern =".(pdf|PDF)",full.names = TRUE) > > > > >> MyFiles <- lapply(FileNames, function(i) extract_tables(i,method = > > > > >> "stream")) > > > > >> if(CalOrd == "Yes"){ > > > > >> MyOFiles <- gsub("(\\s.*)|(.pdf|.PDF)","",basename(FileNames)) > > > > >> MyOFiles <- match(MyOFiles,month.name) > > > > >> MyNFiles <- MyFiles[order(MyOFiles)]} > > > > >> else > > > > >> MyFiles > > > > >> } > > > > >> MyTables <- ExtTable(Path=MyPath,CalOrd = "No") > > > > >> > > > > >> Here is cleaned portion of the output: The whole output consists of 3 > > > > >> lists, each contains 12, 15, and 12 sub-lists. > > > > >> > > > > >> [[2]][[2]] > > > > >> [,1][,2][,3][,4] [,5][,6][,7][,8] > > > > >> [,9] [,10] > > > > >> [1,] "" "Avg." "+_ lo" "n" "Med." "" "Avg." "+_ > > > > >> lo" "n" "Med." > > > > >> [2,] "SiOz" "44.0" "1.26" "375" "44.1" "Nb""4.8" "6.3" > > > > >> "58" "2.7" > > > > >> [3,] "T i O 2" "0.09"
Re: [R] Combine recursive lists in a single list or data frame and write it to file
Hi Ek, It looks to me as though you are not joining the lists into a single list, then calling FillList and then converting to a data frame. If you can send some data (if it's not too big) I can test it and make sure that it works, as it did every time for me. Jim On Thu, Dec 20, 2018 at 2:22 PM Ek Esawi wrote: > > Thank you Jim. I did use unlist with the recursive option which > converted the 3 levels list to a list of 38 matrices. I tried your > earlier function to join the 38 matrices, all of which have different > number of columns and rows, but i kept getting an error. > > fillList<-function(x) { > + maxrows<-max(unlist(lapply(x,length))) > + return(lapply(x,"[",1:maxrows)) > + } > > > > for (i in 1:length(MyTables)) { > + write.table(as.data.frame(fillList(MyTables[i])), > + file = "Temp.txt",append = TRUE,quote = TRUE)} > Error in (function (..., row.names = NULL, check.rows = FALSE, > check.names = TRUE, : > arguments imply differing number of rows: 3, 55, 56, 53, 54, 16, 21, > 23, 50, 24 > > > On Wed, Dec 19, 2018 at 9:36 PM Jim Lemon wrote: > > > > Hi Ek, > > Look at unlist and the argument "recursive". You can step down through > > the levels or a nested list to convert it to a single level list. > > > > Jim > > > > On Thu, Dec 20, 2018 at 1:33 PM Ek Esawi wrote: > > > > > > Thank you Bert. I don't see how unlist will help. I want to combine > > > them but keep the "rectangular structure",e.g. list, data frame, > > > matrix because i want to get the tables in their original form. > > > Unlist converts the whole output to a single vector; unless i am > > > missing something. > > > > > > On Wed, Dec 19, 2018 at 9:10 PM Bert Gunter > > > wrote: > > > > > > > > Does ?unlist not help? Why not? > > > > > > > > Bert > > > > > > > > > > > > On Wed, Dec 19, 2018, 5:13 PM Ek Esawi > > >> > > > >> Hi All— > > > >> > > > >> I am using the R tabulizer package to extract tables from pdf files. > > > >> The output is a set of lists of matrices. The package extracts tables > > > >> and a lot of extra stuff which is nearly impossible to clean with > > > >> RegEx. So, I want to clean it manually. > > > >> To do so I need to (1) combine all lists in a single list or data > > > >> frame and (2) then write the single entity to a text file to edit it. > > > >> I could not figure out how. > > > >> > > > >> I tried something like this but did not work. > > > >> lapply(MyTables, function(x) > > > >> lapply(x,write.table(file="temp.txt",append = TRUE))) > > > >> > > > >> Any help is greatly appreciated. > > > >> > > > >> Here is my code: > > > >> > > > >> install.packages("rJava");library(rJava) > > > >> install.packages("tabulizer");library(tabulizer) > > > >> MyPath <- "C:/Users/name/Documents/tEMP" > > > >> ExtTable <- function (Path,CalOrd){ > > > >> FileNames <- dir(Path, pattern =".(pdf|PDF)",full.names = TRUE) > > > >> MyFiles <- lapply(FileNames, function(i) extract_tables(i,method = > > > >> "stream")) > > > >> if(CalOrd == "Yes"){ > > > >> MyOFiles <- gsub("(\\s.*)|(.pdf|.PDF)","",basename(FileNames)) > > > >> MyOFiles <- match(MyOFiles,month.name) > > > >> MyNFiles <- MyFiles[order(MyOFiles)]} > > > >> else > > > >> MyFiles > > > >> } > > > >> MyTables <- ExtTable(Path=MyPath,CalOrd = "No") > > > >> > > > >> Here is cleaned portion of the output: The whole output consists of 3 > > > >> lists, each contains 12, 15, and 12 sub-lists. > > > >> > > > >> [[2]][[2]] > > > >> [,1][,2][,3][,4] [,5][,6][,7][,8] > > > >> [,9] [,10] > > > >> [1,] "" "Avg." "+_ lo" "n" "Med." "" "Avg." "+_ > > > >> lo" "n" "Med." > > > >> [2,] "SiOz" "44.0" "1.26" "375" "44.1" "Nb""4.8" "6.3" > > > >> "58" "2.7" > > > >> [3,] "T i O 2" "0.09" "0.09" "561" "0.09" "Mo(b)" "50""30" > > > >> "3" "35" > > > >> [4,] "A1203" "2.27" "1.10" "375" "2.20" "Ru(b)" "12.4" "4.1" > > > >> "3" "12" > > > >> [5,] "FeO total" "8.43" "1.14" "375" "8.19" "Pd(b)" "3.9" "2.1" > > > >> "19" "4.1" > > > >> [6,] "MnO" "0.14" "0.03" "366" "0.14" "Ag(b)" "6.8" "8.3" > > > >> "17" "4.8" > > > >> [7,] "MgO" "41.4" "3.00" "375" "41.2" "Cd(b)" "41""14" > > > >> "16" "37" > > > >> [8,] "CaO" "2.15" "1.11" "374" "2.20" "In(b)" "12""4" > > > >> "19" "12" > > > >> [9,] "Na20" "0.24" "0.16" "341" "0.21" "Sn(b)" "54""31" > > > >> "6" "36" > > > >> [10,] "K20" "0.054" "0.11" "330" "0.028" "Sb(b)" "3.9" "3.9" > > > >> "11" "3.2" > > > >> [11,] "P205" "0.056" "0.11" "233" "0.030" "Te(b)" "11""4" > > > >> "18" "10" > > > >> [12,] "Total" "98.88" "" """98.43" "Cs(b)" "10""16" > > > >> "17" "1.5" > > > >> [13,] "" "" "" """" "Ba""33""52" > > > >> "75" "17" > > > >> [14,] "Mg-value" "89.8" "1.1" "375" "90.0" "La""2.60" "5.70" > > > >> "208"
Re: [R] Combine recursive lists in a single list or data frame and write it to file
Thank you Jim. I did use unlist with the recursive option which converted the 3 levels list to a list of 38 matrices. I tried your earlier function to join the 38 matrices, all of which have different number of columns and rows, but i kept getting an error. fillList<-function(x) { + maxrows<-max(unlist(lapply(x,length))) + return(lapply(x,"[",1:maxrows)) + } > > for (i in 1:length(MyTables)) { + write.table(as.data.frame(fillList(MyTables[i])), + file = "Temp.txt",append = TRUE,quote = TRUE)} Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : arguments imply differing number of rows: 3, 55, 56, 53, 54, 16, 21, 23, 50, 24 On Wed, Dec 19, 2018 at 9:36 PM Jim Lemon wrote: > > Hi Ek, > Look at unlist and the argument "recursive". You can step down through > the levels or a nested list to convert it to a single level list. > > Jim > > On Thu, Dec 20, 2018 at 1:33 PM Ek Esawi wrote: > > > > Thank you Bert. I don't see how unlist will help. I want to combine > > them but keep the "rectangular structure",e.g. list, data frame, > > matrix because i want to get the tables in their original form. > > Unlist converts the whole output to a single vector; unless i am > > missing something. > > > > On Wed, Dec 19, 2018 at 9:10 PM Bert Gunter wrote: > > > > > > Does ?unlist not help? Why not? > > > > > > Bert > > > > > > > > > On Wed, Dec 19, 2018, 5:13 PM Ek Esawi > >> > > >> Hi All— > > >> > > >> I am using the R tabulizer package to extract tables from pdf files. > > >> The output is a set of lists of matrices. The package extracts tables > > >> and a lot of extra stuff which is nearly impossible to clean with > > >> RegEx. So, I want to clean it manually. > > >> To do so I need to (1) combine all lists in a single list or data > > >> frame and (2) then write the single entity to a text file to edit it. > > >> I could not figure out how. > > >> > > >> I tried something like this but did not work. > > >> lapply(MyTables, function(x) > > >> lapply(x,write.table(file="temp.txt",append = TRUE))) > > >> > > >> Any help is greatly appreciated. > > >> > > >> Here is my code: > > >> > > >> install.packages("rJava");library(rJava) > > >> install.packages("tabulizer");library(tabulizer) > > >> MyPath <- "C:/Users/name/Documents/tEMP" > > >> ExtTable <- function (Path,CalOrd){ > > >> FileNames <- dir(Path, pattern =".(pdf|PDF)",full.names = TRUE) > > >> MyFiles <- lapply(FileNames, function(i) extract_tables(i,method = > > >> "stream")) > > >> if(CalOrd == "Yes"){ > > >> MyOFiles <- gsub("(\\s.*)|(.pdf|.PDF)","",basename(FileNames)) > > >> MyOFiles <- match(MyOFiles,month.name) > > >> MyNFiles <- MyFiles[order(MyOFiles)]} > > >> else > > >> MyFiles > > >> } > > >> MyTables <- ExtTable(Path=MyPath,CalOrd = "No") > > >> > > >> Here is cleaned portion of the output: The whole output consists of 3 > > >> lists, each contains 12, 15, and 12 sub-lists. > > >> > > >> [[2]][[2]] > > >> [,1][,2][,3][,4] [,5][,6][,7][,8][,9] > > >> [,10] > > >> [1,] "" "Avg." "+_ lo" "n" "Med." "" "Avg." "+_ > > >> lo" "n" "Med." > > >> [2,] "SiOz" "44.0" "1.26" "375" "44.1" "Nb""4.8" "6.3" > > >> "58" "2.7" > > >> [3,] "T i O 2" "0.09" "0.09" "561" "0.09" "Mo(b)" "50""30" > > >> "3" "35" > > >> [4,] "A1203" "2.27" "1.10" "375" "2.20" "Ru(b)" "12.4" "4.1" > > >> "3" "12" > > >> [5,] "FeO total" "8.43" "1.14" "375" "8.19" "Pd(b)" "3.9" "2.1" > > >> "19" "4.1" > > >> [6,] "MnO" "0.14" "0.03" "366" "0.14" "Ag(b)" "6.8" "8.3" > > >> "17" "4.8" > > >> [7,] "MgO" "41.4" "3.00" "375" "41.2" "Cd(b)" "41""14" > > >> "16" "37" > > >> [8,] "CaO" "2.15" "1.11" "374" "2.20" "In(b)" "12""4" > > >> "19" "12" > > >> [9,] "Na20" "0.24" "0.16" "341" "0.21" "Sn(b)" "54""31" > > >> "6" "36" > > >> [10,] "K20" "0.054" "0.11" "330" "0.028" "Sb(b)" "3.9" "3.9" > > >> "11" "3.2" > > >> [11,] "P205" "0.056" "0.11" "233" "0.030" "Te(b)" "11""4" > > >> "18" "10" > > >> [12,] "Total" "98.88" "" """98.43" "Cs(b)" "10""16" > > >> "17" "1.5" > > >> [13,] "" "" "" """" "Ba""33""52" > > >> "75" "17" > > >> [14,] "Mg-value" "89.8" "1.1" "375" "90.0" "La""2.60" "5.70" > > >> "208" "0.77" > > >> [15,] "Ca/AI" "1.28" "1.6" "374" "1.35" "Ce""6.29" "11.7" > > >> "197" "2.08" > > >> [16,] "AI/Ti" "22""29""361" "22""Pr""0.56" "0.87" > > >> "40" "0.21" > > >> [17,] "F e / M n" "60""10""366" "59""Nd""2.67" "4.31" > > >> "162" "1.52" > > >> [18,] "" "" "" """" "Sm""0.47" "0.69" > > >> "214" "0.25" > > >> [19,] "Li""1.5" "0.3" "6" "1.5" "Eu""0.16" "0.21" > > >> "201" "0.097" > > >> [20,] "B" "0.53" "0.07" "6"
Re: [R] Combine recursive lists in a single list or data frame and write it to file
Hi Ek, Look at unlist and the argument "recursive". You can step down through the levels or a nested list to convert it to a single level list. Jim On Thu, Dec 20, 2018 at 1:33 PM Ek Esawi wrote: > > Thank you Bert. I don't see how unlist will help. I want to combine > them but keep the "rectangular structure",e.g. list, data frame, > matrix because i want to get the tables in their original form. > Unlist converts the whole output to a single vector; unless i am > missing something. > > On Wed, Dec 19, 2018 at 9:10 PM Bert Gunter wrote: > > > > Does ?unlist not help? Why not? > > > > Bert > > > > > > On Wed, Dec 19, 2018, 5:13 PM Ek Esawi >> > >> Hi All— > >> > >> I am using the R tabulizer package to extract tables from pdf files. > >> The output is a set of lists of matrices. The package extracts tables > >> and a lot of extra stuff which is nearly impossible to clean with > >> RegEx. So, I want to clean it manually. > >> To do so I need to (1) combine all lists in a single list or data > >> frame and (2) then write the single entity to a text file to edit it. > >> I could not figure out how. > >> > >> I tried something like this but did not work. > >> lapply(MyTables, function(x) > >> lapply(x,write.table(file="temp.txt",append = TRUE))) > >> > >> Any help is greatly appreciated. > >> > >> Here is my code: > >> > >> install.packages("rJava");library(rJava) > >> install.packages("tabulizer");library(tabulizer) > >> MyPath <- "C:/Users/name/Documents/tEMP" > >> ExtTable <- function (Path,CalOrd){ > >> FileNames <- dir(Path, pattern =".(pdf|PDF)",full.names = TRUE) > >> MyFiles <- lapply(FileNames, function(i) extract_tables(i,method = > >> "stream")) > >> if(CalOrd == "Yes"){ > >> MyOFiles <- gsub("(\\s.*)|(.pdf|.PDF)","",basename(FileNames)) > >> MyOFiles <- match(MyOFiles,month.name) > >> MyNFiles <- MyFiles[order(MyOFiles)]} > >> else > >> MyFiles > >> } > >> MyTables <- ExtTable(Path=MyPath,CalOrd = "No") > >> > >> Here is cleaned portion of the output: The whole output consists of 3 > >> lists, each contains 12, 15, and 12 sub-lists. > >> > >> [[2]][[2]] > >> [,1][,2][,3][,4] [,5][,6][,7][,8][,9] > >> [,10] > >> [1,] "" "Avg." "+_ lo" "n" "Med." "" "Avg." "+_ > >> lo" "n" "Med." > >> [2,] "SiOz" "44.0" "1.26" "375" "44.1" "Nb""4.8" "6.3" > >> "58" "2.7" > >> [3,] "T i O 2" "0.09" "0.09" "561" "0.09" "Mo(b)" "50""30" > >> "3" "35" > >> [4,] "A1203" "2.27" "1.10" "375" "2.20" "Ru(b)" "12.4" "4.1" > >> "3" "12" > >> [5,] "FeO total" "8.43" "1.14" "375" "8.19" "Pd(b)" "3.9" "2.1" > >> "19" "4.1" > >> [6,] "MnO" "0.14" "0.03" "366" "0.14" "Ag(b)" "6.8" "8.3" > >> "17" "4.8" > >> [7,] "MgO" "41.4" "3.00" "375" "41.2" "Cd(b)" "41""14" > >> "16" "37" > >> [8,] "CaO" "2.15" "1.11" "374" "2.20" "In(b)" "12""4" > >> "19" "12" > >> [9,] "Na20" "0.24" "0.16" "341" "0.21" "Sn(b)" "54""31" > >> "6" "36" > >> [10,] "K20" "0.054" "0.11" "330" "0.028" "Sb(b)" "3.9" "3.9" > >> "11" "3.2" > >> [11,] "P205" "0.056" "0.11" "233" "0.030" "Te(b)" "11""4" > >> "18" "10" > >> [12,] "Total" "98.88" "" """98.43" "Cs(b)" "10""16" > >> "17" "1.5" > >> [13,] "" "" "" """" "Ba""33""52" > >> "75" "17" > >> [14,] "Mg-value" "89.8" "1.1" "375" "90.0" "La""2.60" "5.70" > >> "208" "0.77" > >> [15,] "Ca/AI" "1.28" "1.6" "374" "1.35" "Ce""6.29" "11.7" > >> "197" "2.08" > >> [16,] "AI/Ti" "22""29""361" "22""Pr""0.56" "0.87" > >> "40" "0.21" > >> [17,] "F e / M n" "60""10""366" "59""Nd""2.67" "4.31" > >> "162" "1.52" > >> [18,] "" "" "" """" "Sm""0.47" "0.69" > >> "214" "0.25" > >> [19,] "Li""1.5" "0.3" "6" "1.5" "Eu""0.16" "0.21" > >> "201" "0.097" > >> [20,] "B" "0.53" "0.07" "6" "0.55" "Gd""0.60" "0.83" > >> "67" "0.31" > >> [21,] "C" "110" "50""13" "93""Tb""0.070" > >> "0.064" "146" "0.056" > >> [22,] "F" "88""71""15" "100" "Dy""0.51" "0.35" > >> "58" "0.47" > >> [23,] "S" "157" "77""22" "152" "Ho""0.12" "0.14" > >> "54" "0.090" > >> [24,] "C1""53""45""15" "75""Er""0.30" "0.22" > >> "52" "0.28" > >> [25,] "Sc""12.2" "6.4" "220" "12.0" "Tm""0.038" > >> "0.026" "40" "0.035" > >> [26,] "V" "56""21""132" "53""Yb""0.26" "0.14" > >> "201" "0.27" > >> [27,] "Cr""2690" "705" "325" "2690" "Lu""0.043" > >> "0.023" "172" "0.045" > >> [28,] "Co""112" "10""166" "111" "Hf""0.27" "0.30" > >> "71" "0.17" > >> [29,] "Ni""2160" "304" "308" "2140" "Ta""0.40" "0.51" > >> "38" "0.23" > >> [30,] "Cu""11""9" "94" "9"
Re: [R] Combine recursive lists in a single list or data frame and write it to file
Thank you Bert. I don't see how unlist will help. I want to combine them but keep the "rectangular structure",e.g. list, data frame, matrix because i want to get the tables in their original form. Unlist converts the whole output to a single vector; unless i am missing something. On Wed, Dec 19, 2018 at 9:10 PM Bert Gunter wrote: > > Does ?unlist not help? Why not? > > Bert > > > On Wed, Dec 19, 2018, 5:13 PM Ek Esawi > >> Hi All— >> >> I am using the R tabulizer package to extract tables from pdf files. >> The output is a set of lists of matrices. The package extracts tables >> and a lot of extra stuff which is nearly impossible to clean with >> RegEx. So, I want to clean it manually. >> To do so I need to (1) combine all lists in a single list or data >> frame and (2) then write the single entity to a text file to edit it. >> I could not figure out how. >> >> I tried something like this but did not work. >> lapply(MyTables, function(x) >> lapply(x,write.table(file="temp.txt",append = TRUE))) >> >> Any help is greatly appreciated. >> >> Here is my code: >> >> install.packages("rJava");library(rJava) >> install.packages("tabulizer");library(tabulizer) >> MyPath <- "C:/Users/name/Documents/tEMP" >> ExtTable <- function (Path,CalOrd){ >> FileNames <- dir(Path, pattern =".(pdf|PDF)",full.names = TRUE) >> MyFiles <- lapply(FileNames, function(i) extract_tables(i,method = >> "stream")) >> if(CalOrd == "Yes"){ >> MyOFiles <- gsub("(\\s.*)|(.pdf|.PDF)","",basename(FileNames)) >> MyOFiles <- match(MyOFiles,month.name) >> MyNFiles <- MyFiles[order(MyOFiles)]} >> else >> MyFiles >> } >> MyTables <- ExtTable(Path=MyPath,CalOrd = "No") >> >> Here is cleaned portion of the output: The whole output consists of 3 >> lists, each contains 12, 15, and 12 sub-lists. >> >> [[2]][[2]] >> [,1][,2][,3][,4] [,5][,6][,7][,8][,9] >> [,10] >> [1,] "" "Avg." "+_ lo" "n" "Med." "" "Avg." "+_ >> lo" "n" "Med." >> [2,] "SiOz" "44.0" "1.26" "375" "44.1" "Nb""4.8" "6.3" >> "58" "2.7" >> [3,] "T i O 2" "0.09" "0.09" "561" "0.09" "Mo(b)" "50""30" >> "3" "35" >> [4,] "A1203" "2.27" "1.10" "375" "2.20" "Ru(b)" "12.4" "4.1" >> "3" "12" >> [5,] "FeO total" "8.43" "1.14" "375" "8.19" "Pd(b)" "3.9" "2.1" >> "19" "4.1" >> [6,] "MnO" "0.14" "0.03" "366" "0.14" "Ag(b)" "6.8" "8.3" >> "17" "4.8" >> [7,] "MgO" "41.4" "3.00" "375" "41.2" "Cd(b)" "41""14" >> "16" "37" >> [8,] "CaO" "2.15" "1.11" "374" "2.20" "In(b)" "12""4" >> "19" "12" >> [9,] "Na20" "0.24" "0.16" "341" "0.21" "Sn(b)" "54""31" >> "6" "36" >> [10,] "K20" "0.054" "0.11" "330" "0.028" "Sb(b)" "3.9" "3.9" >> "11" "3.2" >> [11,] "P205" "0.056" "0.11" "233" "0.030" "Te(b)" "11""4" >> "18" "10" >> [12,] "Total" "98.88" "" """98.43" "Cs(b)" "10""16" >> "17" "1.5" >> [13,] "" "" "" """" "Ba""33""52" >> "75" "17" >> [14,] "Mg-value" "89.8" "1.1" "375" "90.0" "La""2.60" "5.70" >> "208" "0.77" >> [15,] "Ca/AI" "1.28" "1.6" "374" "1.35" "Ce""6.29" "11.7" >> "197" "2.08" >> [16,] "AI/Ti" "22""29""361" "22""Pr""0.56" "0.87" >> "40" "0.21" >> [17,] "F e / M n" "60""10""366" "59""Nd""2.67" "4.31" >> "162" "1.52" >> [18,] "" "" "" """" "Sm""0.47" "0.69" >> "214" "0.25" >> [19,] "Li""1.5" "0.3" "6" "1.5" "Eu""0.16" "0.21" >> "201" "0.097" >> [20,] "B" "0.53" "0.07" "6" "0.55" "Gd""0.60" "0.83" >> "67" "0.31" >> [21,] "C" "110" "50""13" "93""Tb""0.070" >> "0.064" "146" "0.056" >> [22,] "F" "88""71""15" "100" "Dy""0.51" "0.35" >> "58" "0.47" >> [23,] "S" "157" "77""22" "152" "Ho""0.12" "0.14" >> "54" "0.090" >> [24,] "C1""53""45""15" "75""Er""0.30" "0.22" >> "52" "0.28" >> [25,] "Sc""12.2" "6.4" "220" "12.0" "Tm""0.038" >> "0.026" "40" "0.035" >> [26,] "V" "56""21""132" "53""Yb""0.26" "0.14" >> "201" "0.27" >> [27,] "Cr""2690" "705" "325" "2690" "Lu""0.043" >> "0.023" "172" "0.045" >> [28,] "Co""112" "10""166" "111" "Hf""0.27" "0.30" >> "71" "0.17" >> [29,] "Ni""2160" "304" "308" "2140" "Ta""0.40" "0.51" >> "38" "0.23" >> [30,] "Cu""11""9" "94" "9" "W(b)" "7.2" "5.2" >> "6" "4.0" >> [31,] "Zn""65""20""129" "60""Re(b)" "0.13" "0.11" >> "18" "0.09" >> [32,] "Ga""2.4" "1.3" "49" "2.4" "Os(b)" "4.0" "1.8" >> "18" "3.7" >> [33,] "Ge""0.96" "0.19" "19" "0.92" "Ir(b)" "3.7" "0.9" >> "34" "3.0" >> [34,] "As""0.11" "0.07" "7" "0.10" "Pt(b)" "7" "-" >> "1" "-" >> [35,] "Se""0.041"
Re: [R] Combine recursive lists in a single list or data frame and write it to file
Does ?unlist not help? Why not? Bert On Wed, Dec 19, 2018, 5:13 PM Ek Esawi Hi All— > > I am using the R tabulizer package to extract tables from pdf files. > The output is a set of lists of matrices. The package extracts tables > and a lot of extra stuff which is nearly impossible to clean with > RegEx. So, I want to clean it manually. > To do so I need to (1) combine all lists in a single list or data > frame and (2) then write the single entity to a text file to edit it. > I could not figure out how. > > I tried something like this but did not work. > lapply(MyTables, function(x) > lapply(x,write.table(file="temp.txt",append = TRUE))) > > Any help is greatly appreciated. > > Here is my code: > > install.packages("rJava");library(rJava) > install.packages("tabulizer");library(tabulizer) > MyPath <- "C:/Users/name/Documents/tEMP" > ExtTable <- function (Path,CalOrd){ > FileNames <- dir(Path, pattern =".(pdf|PDF)",full.names = TRUE) > MyFiles <- lapply(FileNames, function(i) extract_tables(i,method = > "stream")) > if(CalOrd == "Yes"){ > MyOFiles <- gsub("(\\s.*)|(.pdf|.PDF)","",basename(FileNames)) > MyOFiles <- match(MyOFiles,month.name) > MyNFiles <- MyFiles[order(MyOFiles)]} > else > MyFiles > } > MyTables <- ExtTable(Path=MyPath,CalOrd = "No") > > Here is cleaned portion of the output: The whole output consists of 3 > lists, each contains 12, 15, and 12 sub-lists. > > [[2]][[2]] > [,1][,2][,3][,4] [,5][,6][,7][,8][,9] > [,10] > [1,] "" "Avg." "+_ lo" "n" "Med." "" "Avg." "+_ > lo" "n" "Med." > [2,] "SiOz" "44.0" "1.26" "375" "44.1" "Nb""4.8" "6.3" > "58" "2.7" > [3,] "T i O 2" "0.09" "0.09" "561" "0.09" "Mo(b)" "50""30" > "3" "35" > [4,] "A1203" "2.27" "1.10" "375" "2.20" "Ru(b)" "12.4" "4.1" > "3" "12" > [5,] "FeO total" "8.43" "1.14" "375" "8.19" "Pd(b)" "3.9" "2.1" > "19" "4.1" > [6,] "MnO" "0.14" "0.03" "366" "0.14" "Ag(b)" "6.8" "8.3" > "17" "4.8" > [7,] "MgO" "41.4" "3.00" "375" "41.2" "Cd(b)" "41""14" > "16" "37" > [8,] "CaO" "2.15" "1.11" "374" "2.20" "In(b)" "12""4" > "19" "12" > [9,] "Na20" "0.24" "0.16" "341" "0.21" "Sn(b)" "54""31" > "6" "36" > [10,] "K20" "0.054" "0.11" "330" "0.028" "Sb(b)" "3.9" "3.9" > "11" "3.2" > [11,] "P205" "0.056" "0.11" "233" "0.030" "Te(b)" "11""4" > "18" "10" > [12,] "Total" "98.88" "" """98.43" "Cs(b)" "10""16" > "17" "1.5" > [13,] "" "" "" """" "Ba""33""52" > "75" "17" > [14,] "Mg-value" "89.8" "1.1" "375" "90.0" "La""2.60" "5.70" > "208" "0.77" > [15,] "Ca/AI" "1.28" "1.6" "374" "1.35" "Ce""6.29" "11.7" > "197" "2.08" > [16,] "AI/Ti" "22""29""361" "22""Pr""0.56" "0.87" > "40" "0.21" > [17,] "F e / M n" "60""10""366" "59""Nd""2.67" "4.31" > "162" "1.52" > [18,] "" "" "" """" "Sm""0.47" "0.69" > "214" "0.25" > [19,] "Li""1.5" "0.3" "6" "1.5" "Eu""0.16" "0.21" > "201" "0.097" > [20,] "B" "0.53" "0.07" "6" "0.55" "Gd""0.60" "0.83" > "67" "0.31" > [21,] "C" "110" "50""13" "93""Tb""0.070" > "0.064" "146" "0.056" > [22,] "F" "88""71""15" "100" "Dy""0.51" "0.35" > "58" "0.47" > [23,] "S" "157" "77""22" "152" "Ho""0.12" "0.14" > "54" "0.090" > [24,] "C1""53""45""15" "75""Er""0.30" "0.22" > "52" "0.28" > [25,] "Sc""12.2" "6.4" "220" "12.0" "Tm""0.038" > "0.026" "40" "0.035" > [26,] "V" "56""21""132" "53""Yb""0.26" "0.14" > "201" "0.27" > [27,] "Cr""2690" "705" "325" "2690" "Lu""0.043" > "0.023" "172" "0.045" > [28,] "Co""112" "10""166" "111" "Hf""0.27" "0.30" > "71" "0.17" > [29,] "Ni""2160" "304" "308" "2140" "Ta""0.40" "0.51" > "38" "0.23" > [30,] "Cu""11""9" "94" "9" "W(b)" "7.2" "5.2" > "6" "4.0" > [31,] "Zn""65""20""129" "60""Re(b)" "0.13" "0.11" > "18" "0.09" > [32,] "Ga""2.4" "1.3" "49" "2.4" "Os(b)" "4.0" "1.8" > "18" "3.7" > [33,] "Ge""0.96" "0.19" "19" "0.92" "Ir(b)" "3.7" "0.9" > "34" "3.0" > [34,] "As""0.11" "0.07" "7" "0.10" "Pt(b)" "7" "-" > "1" "-" > [35,] "Se""0.041" "0.056" "18" "0.025" "Au(b)" "0.65" "0.53" > "30" "0.5" > [36,] "Br""0.01" "0.01" "6" "0.01" "Tl(b)" "1.2" "1.0" > "13" "0.9" > [37,] "Rb""1,9" "4.8" "97" "0.38" "Pb""0.16" "0.11" > "17" "0.16" > [38,] "Sr""49""60""110" "20""Bi(b)" "1.7" "0.7" > "13" "1.6" > [39,] "Y" "4.4" "5.5" "86" "3.1" "Th*" "0.71" "1.2" > "71" "0.22" > [40,] "Zr""21""42""82" "8.0" "U"
[R] Combine recursive lists in a single list or data frame and write it to file
Hi All— I am using the R tabulizer package to extract tables from pdf files. The output is a set of lists of matrices. The package extracts tables and a lot of extra stuff which is nearly impossible to clean with RegEx. So, I want to clean it manually. To do so I need to (1) combine all lists in a single list or data frame and (2) then write the single entity to a text file to edit it. I could not figure out how. I tried something like this but did not work. lapply(MyTables, function(x) lapply(x,write.table(file="temp.txt",append = TRUE))) Any help is greatly appreciated. Here is my code: install.packages("rJava");library(rJava) install.packages("tabulizer");library(tabulizer) MyPath <- "C:/Users/name/Documents/tEMP" ExtTable <- function (Path,CalOrd){ FileNames <- dir(Path, pattern =".(pdf|PDF)",full.names = TRUE) MyFiles <- lapply(FileNames, function(i) extract_tables(i,method = "stream")) if(CalOrd == "Yes"){ MyOFiles <- gsub("(\\s.*)|(.pdf|.PDF)","",basename(FileNames)) MyOFiles <- match(MyOFiles,month.name) MyNFiles <- MyFiles[order(MyOFiles)]} else MyFiles } MyTables <- ExtTable(Path=MyPath,CalOrd = "No") Here is cleaned portion of the output: The whole output consists of 3 lists, each contains 12, 15, and 12 sub-lists. [[2]][[2]] [,1][,2][,3][,4] [,5][,6][,7][,8][,9] [,10] [1,] "" "Avg." "+_ lo" "n" "Med." "" "Avg." "+_ lo" "n" "Med." [2,] "SiOz" "44.0" "1.26" "375" "44.1" "Nb""4.8" "6.3" "58" "2.7" [3,] "T i O 2" "0.09" "0.09" "561" "0.09" "Mo(b)" "50""30" "3" "35" [4,] "A1203" "2.27" "1.10" "375" "2.20" "Ru(b)" "12.4" "4.1" "3" "12" [5,] "FeO total" "8.43" "1.14" "375" "8.19" "Pd(b)" "3.9" "2.1" "19" "4.1" [6,] "MnO" "0.14" "0.03" "366" "0.14" "Ag(b)" "6.8" "8.3" "17" "4.8" [7,] "MgO" "41.4" "3.00" "375" "41.2" "Cd(b)" "41""14" "16" "37" [8,] "CaO" "2.15" "1.11" "374" "2.20" "In(b)" "12""4" "19" "12" [9,] "Na20" "0.24" "0.16" "341" "0.21" "Sn(b)" "54""31" "6" "36" [10,] "K20" "0.054" "0.11" "330" "0.028" "Sb(b)" "3.9" "3.9" "11" "3.2" [11,] "P205" "0.056" "0.11" "233" "0.030" "Te(b)" "11""4" "18" "10" [12,] "Total" "98.88" "" """98.43" "Cs(b)" "10""16" "17" "1.5" [13,] "" "" "" """" "Ba""33""52" "75" "17" [14,] "Mg-value" "89.8" "1.1" "375" "90.0" "La""2.60" "5.70" "208" "0.77" [15,] "Ca/AI" "1.28" "1.6" "374" "1.35" "Ce""6.29" "11.7" "197" "2.08" [16,] "AI/Ti" "22""29""361" "22""Pr""0.56" "0.87" "40" "0.21" [17,] "F e / M n" "60""10""366" "59""Nd""2.67" "4.31" "162" "1.52" [18,] "" "" "" """" "Sm""0.47" "0.69" "214" "0.25" [19,] "Li""1.5" "0.3" "6" "1.5" "Eu""0.16" "0.21" "201" "0.097" [20,] "B" "0.53" "0.07" "6" "0.55" "Gd""0.60" "0.83" "67" "0.31" [21,] "C" "110" "50""13" "93""Tb""0.070" "0.064" "146" "0.056" [22,] "F" "88""71""15" "100" "Dy""0.51" "0.35" "58" "0.47" [23,] "S" "157" "77""22" "152" "Ho""0.12" "0.14" "54" "0.090" [24,] "C1""53""45""15" "75""Er""0.30" "0.22" "52" "0.28" [25,] "Sc""12.2" "6.4" "220" "12.0" "Tm""0.038" "0.026" "40" "0.035" [26,] "V" "56""21""132" "53""Yb""0.26" "0.14" "201" "0.27" [27,] "Cr""2690" "705" "325" "2690" "Lu""0.043" "0.023" "172" "0.045" [28,] "Co""112" "10""166" "111" "Hf""0.27" "0.30" "71" "0.17" [29,] "Ni""2160" "304" "308" "2140" "Ta""0.40" "0.51" "38" "0.23" [30,] "Cu""11""9" "94" "9" "W(b)" "7.2" "5.2" "6" "4.0" [31,] "Zn""65""20""129" "60""Re(b)" "0.13" "0.11" "18" "0.09" [32,] "Ga""2.4" "1.3" "49" "2.4" "Os(b)" "4.0" "1.8" "18" "3.7" [33,] "Ge""0.96" "0.19" "19" "0.92" "Ir(b)" "3.7" "0.9" "34" "3.0" [34,] "As""0.11" "0.07" "7" "0.10" "Pt(b)" "7" "-" "1" "-" [35,] "Se""0.041" "0.056" "18" "0.025" "Au(b)" "0.65" "0.53" "30" "0.5" [36,] "Br""0.01" "0.01" "6" "0.01" "Tl(b)" "1.2" "1.0" "13" "0.9" [37,] "Rb""1,9" "4.8" "97" "0.38" "Pb""0.16" "0.11" "17" "0.16" [38,] "Sr""49""60""110" "20""Bi(b)" "1.7" "0.7" "13" "1.6" [39,] "Y" "4.4" "5.5" "86" "3.1" "Th*" "0.71" "1.2" "71" "0.22" [40,] "Zr""21""42""82" "8.0" "U" "0.12" "0.23" "48" "0.040" [[2]][[4]] [,1] [,2] [,3] [,4] [,5] [,6] [1,] "" "Spinel peridotites" "" "Garnet peridotites" "" "Primitive" [2,] "" "Avg. Meal." "M-A sp" "M-A gt B-M" "Jordan" "mantle" [3,] "SiO 2""44.0
Re: [R] Combine lists into a data frame or append them to a text file
Hi Jim, Thanks again. Actually i changed my code where the lists are not nested. Your code works, as you said for the example, but still is not working for my lists (30). My lists have different columns and rows and several are NULL; plus there are many blank space which i suppose don't make much difference, but not sure. I will try to send an actual output.. Thanks again---EK. > for (i in 1:length(MyTables)) { + write.table(as.data.frame(fillList(MyTables[i])), + file = "Temp.txt",append = TRUE,quote = TRUE)} Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : arguments imply differing number of rows: 4, 50, 53, 8, 20 In addition: There were 20 warnings (use warnings() to see t On Sun, Dec 16, 2018 at 4:10 PM Jim Lemon wrote: > > Hi Ek, > Okay, you got me. I didn't write the function to handle nested lists. > If you combine your initial lists into a single level list, I think it > will work: > > fillList<-function(x) { > maxrows<-max(unlist(lapply(x,length))) > return(lapply(x,"[",1:maxrows)) > } > AA <- list(a=c(1,2,3,4),b = c("a","b","c")) > BB <- list(c=c(1,2,3,4,5),d=c("a","b","c","d","e")) > mylist <- c(AA,BB) > mydf > Jim > > On Mon, Dec 17, 2018 at 12:45 AM Ek Esawi wrote: > > > > Thank you Jim and Bert, > > > > I tried Jim's function and it works. But here is an example just in case. > > > > AA <- list(a=c(1,2,3,4),b = c("a","b","c")) > > BB <- list(c=c(1,2,3,4,5),d=c("a","b","c","d","e")) > > mylist <- (list(AA,BB)) > > > > lapply(mylist,function(x) write.table(x,file = test.txt)) > > Show Traceback > > > > Error in (function (..., row.names = NULL, check.rows = FALSE, > > check.names = TRUE, : > > arguments imply differing number of rows: 4, 3 > > > > On Sun, Dec 16, 2018 at 1:03 AM Jim Lemon wrote: > > > > > > Hi Ek, > > > I thought there would be a simple fix for this, but had to write a > > > little function: > > > > > > fillList<-function(x) { > > > maxrows<-max(unlist(lapply(x,length))) > > > return(lapply(x,"[",1:maxrows)) > > > } > > > > > > that fills up the rows of each list with NAs. I got the expected result > > > with: > > > > > > testlist<-list(a=1:8,b=1:9,c=1:10) > > > as.data.frame(fillList(testlist)) > > > > > > so: > > > > > > for (i in 1:length(MyTables)) { > > > write.table(as.data.frame(fillList(MyTables[i])), > > > file = "Temp.txt",append = TRUE,quote = TRUE) > > > > > > may do the job. > > > > > > Jim > > > > > > On Sun, Dec 16, 2018 at 2:28 PM Ek Esawi wrote: > > > > > > > > Hi All, > > > > > > > > I have an R object that is made up of N number of lists which are all > > > > of different number of columns and rows. I want to combine the N > > > > lists into a single data frame or write (append) them into text file. > > > > I hope the question is clear and doesn’t require an example. I am > > > > hoping to accomplish this using base R functions. > > > > Below is what I tried but both gave me the same error which I do > > > > understand, I think, but I don’t know how to fix it. My R object is > > > > MyTables > > > > > > > > lapply(MyTables, function(x) write.table(x, file = "Temp.txt",append = > > > > TRUE )) > > > > OR > > > > for (i in 1:length(MyTables)) { > > > > write.table(MyTables[i], file = "Temp.txt",append = TRUE,quote = TRUE) > > > > > > > > the error > > > > Error in (function (..., row.names = NULL, check.rows = FALSE, > > > > check.names = TRUE, : > > > > arguments imply differing number of rows: 51, 8, 30 > > > > > > > > Thanks--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] Combine lists into a data frame or append them to a text file
I tried Jim's function and it works. But here is an example just in case. AA <- list(a=c(1,2,3,4),b = c("a","b","c")) BB <- list(c=c(1,2,3,4,5),d=c("a","b","c","d","e")) mylist <- (list(AA,BB)) lapply(mylist,function(x) write.table(x,file = test.txt)) Show Traceback Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : arguments imply differing number of rows: 4, 3 On Sun, Dec 16, 2018 at 8:45 AM Ek Esawi wrote: > > Thank you Jim and Bert, > > I tried Jim's function and it works. But here is an example just in case. > > AA <- list(a=c(1,2,3,4),b = c("a","b","c")) > BB <- list(c=c(1,2,3,4,5),d=c("a","b","c","d","e")) > mylist <- (list(AA,BB)) > > lapply(mylist,function(x) write.table(x,file = test.txt)) > Show Traceback > > Error in (function (..., row.names = NULL, check.rows = FALSE, > check.names = TRUE, : > arguments imply differing number of rows: 4, 3 > > On Sun, Dec 16, 2018 at 1:03 AM Jim Lemon wrote: > > > > Hi Ek, > > I thought there would be a simple fix for this, but had to write a > > little function: > > > > fillList<-function(x) { > > maxrows<-max(unlist(lapply(x,length))) > > return(lapply(x,"[",1:maxrows)) > > } > > > > that fills up the rows of each list with NAs. I got the expected result > > with: > > > > testlist<-list(a=1:8,b=1:9,c=1:10) > > as.data.frame(fillList(testlist)) > > > > so: > > > > for (i in 1:length(MyTables)) { > > write.table(as.data.frame(fillList(MyTables[i])), > > file = "Temp.txt",append = TRUE,quote = TRUE) > > > > may do the job. > > > > Jim > > > > On Sun, Dec 16, 2018 at 2:28 PM Ek Esawi wrote: > > > > > > Hi All, > > > > > > I have an R object that is made up of N number of lists which are all > > > of different number of columns and rows. I want to combine the N > > > lists into a single data frame or write (append) them into text file. > > > I hope the question is clear and doesn’t require an example. I am > > > hoping to accomplish this using base R functions. > > > Below is what I tried but both gave me the same error which I do > > > understand, I think, but I don’t know how to fix it. My R object is > > > MyTables > > > > > > lapply(MyTables, function(x) write.table(x, file = "Temp.txt",append = > > > TRUE )) > > > OR > > > for (i in 1:length(MyTables)) { > > > write.table(MyTables[i], file = "Temp.txt",append = TRUE,quote = TRUE) > > > > > > the error > > > Error in (function (..., row.names = NULL, check.rows = FALSE, > > > check.names = TRUE, : > > > arguments imply differing number of rows: 51, 8, 30 > > > > > > Thanks--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] Combine lists into a data frame or append them to a text file
Hi Ek, I thought there would be a simple fix for this, but had to write a little function: fillList<-function(x) { maxrows<-max(unlist(lapply(x,length))) return(lapply(x,"[",1:maxrows)) } that fills up the rows of each list with NAs. I got the expected result with: testlist<-list(a=1:8,b=1:9,c=1:10) as.data.frame(fillList(testlist)) so: for (i in 1:length(MyTables)) { write.table(as.data.frame(fillList(MyTables[i])), file = "Temp.txt",append = TRUE,quote = TRUE) may do the job. Jim On Sun, Dec 16, 2018 at 2:28 PM Ek Esawi wrote: > > Hi All, > > I have an R object that is made up of N number of lists which are all > of different number of columns and rows. I want to combine the N > lists into a single data frame or write (append) them into text file. > I hope the question is clear and doesn’t require an example. I am > hoping to accomplish this using base R functions. > Below is what I tried but both gave me the same error which I do > understand, I think, but I don’t know how to fix it. My R object is > MyTables > > lapply(MyTables, function(x) write.table(x, file = "Temp.txt",append = TRUE )) > OR > for (i in 1:length(MyTables)) { > write.table(MyTables[i], file = "Temp.txt",append = TRUE,quote = TRUE) > > the error > Error in (function (..., row.names = NULL, check.rows = FALSE, > check.names = TRUE, : > arguments imply differing number of rows: 51, 8, 30 > > Thanks--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] Combine lists into a data frame or append them to a text file
FWIW, I had no trouble writing a test case to a file with either version of your code. As we have no idea what your data look like, I don't know how anyone can diagnose the problem. But maybe I'm wrong and someone else will recognize the issue. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Sat, Dec 15, 2018 at 7:28 PM Ek Esawi wrote: > Hi All, > > I have an R object that is made up of N number of lists which are all > of different number of columns and rows. I want to combine the N > lists into a single data frame or write (append) them into text file. > I hope the question is clear and doesn’t require an example. I am > hoping to accomplish this using base R functions. > Below is what I tried but both gave me the same error which I do > understand, I think, but I don’t know how to fix it. My R object is > MyTables > > lapply(MyTables, function(x) write.table(x, file = "Temp.txt",append = > TRUE )) > OR > for (i in 1:length(MyTables)) { > write.table(MyTables[i], file = "Temp.txt",append = TRUE,quote = TRUE) > > the error > Error in (function (..., row.names = NULL, check.rows = FALSE, > check.names = TRUE, : > arguments imply differing number of rows: 51, 8, 30 > > Thanks--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. > [[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] Combine lists into a data frame or append them to a text file
Hi All, I have an R object that is made up of N number of lists which are all of different number of columns and rows. I want to combine the N lists into a single data frame or write (append) them into text file. I hope the question is clear and doesn’t require an example. I am hoping to accomplish this using base R functions. Below is what I tried but both gave me the same error which I do understand, I think, but I don’t know how to fix it. My R object is MyTables lapply(MyTables, function(x) write.table(x, file = "Temp.txt",append = TRUE )) OR for (i in 1:length(MyTables)) { write.table(MyTables[i], file = "Temp.txt",append = TRUE,quote = TRUE) the error Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : arguments imply differing number of rows: 51, 8, 30 Thanks--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.
Re: [R] Combine by columns a vector with another vector that is constant across rows
For what it's worth, for larger vectors, and following on from your observation that the do.call() approach is faster, the following provides some modest additional speedup: cbind(x,t(do.call(cbind, lapply(x, function(y) vec Rgds, Eric On Tue, Jul 3, 2018 at 8:12 PM, Viechtbauer, Wolfgang (SP) < wolfgang.viechtba...@maastrichtuniversity.nl> wrote: > Thanks for all of the suggestions. I did some benchmarking: > > library(microbenchmark) > > x <- 1:5 > vec <- c(2,4,3) > > fastWolfgang <- function(v, vec) >matrix(c(v, rep(vec, each = length(v))), nrow = length(v)) > > microbenchmark(cbind(x, t(replicate(length(x), vec))), >t(sapply(x, function(x) c(x, vec))), >do.call(rbind, lapply(x, function(x) c(x, vec))), >t(mapply(c, x, MoreArgs=list(vec))), >Reduce(cbind, vec, x), >Reduce(cbind2, vec, x), >fastWolfgang(x, vec), times=1L) > > Jeff's approach is fastest, but Gabor's Reduce(cbind, vec, x) is close > (and I really like its simplicity); and very similar to the do.call() > approach. > > Interestingly, for larger vectors, such as: > > x <- 1:50 > vec <- sample(1:100, 200, replace=TRUE) > > the do.call() approach is the fastest. > > Best, > Wolfgang > > >-Original Message- > >From: Jeff Newmiller [mailto:jdnew...@dcn.davis.ca.us] > >Sent: Tuesday, 03 July, 2018 17:48 > >To: r-help@r-project.org; Viechtbauer, Wolfgang (SP); r-help@r- > >project.org > >Subject: Re: [R] Combine by columns a vector with another vector that is > >constant across rows > > > >Sorry trying again... > > > >fastWolfgang <- function( v, vec ) { > > matrix( c( v, rep( vec, each = length( v ) ) ) > > , nrow = length( v ) ) > >} > > > >On July 3, 2018 8:21:47 AM PDT, Jeff Newmiller > >wrote: > >>Gabor's solution seems to optimize 'simpler'. > >> > >>More efficient is to learn that in R a vector is not a matrix, but a > >>matrix is just an ornamented vector. > >> > >>fastWolfgang <- function( v, vec ) { > >> matrix( c( v, rep( vec, length( v ) ) ) > >> , now = length( v ) ) > >>} > >> > >>On July 3, 2018 6:28:45 AM PDT, "Viechtbauer, Wolfgang (SP)" > >> wrote: > >>>Hi All, > >>> > >>>I have one vector that I want to combine with another vector and that > >>>other vector should be the same for every row in the combined matrix. > >>>This obviously does not work: > >>> > >>>vec <- c(2,4,3) > >>>cbind(1:5, vec) > >>> > >>>This does, but requires me to specify the correct value for 'n' in > >>>replicate(): > >>> > >>>cbind(1:5, t(replicate(5, vec))) > >>> > >>>Other ways that do not require this are: > >>> > >>>t(sapply(1:5, function(x) c(x, vec))) > >>>do.call(rbind, lapply(1:5, function(x) c(x, vec))) > >>>t(mapply(c, 1:5, MoreArgs=list(vec))) > >>> > >>>I wonder if there is a simpler / more efficient way of doing this. > >>> > >>>Best, > >>>Wolfgang > __ > 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] Combine by columns a vector with another vector that is constant across rows
Thanks for all of the suggestions. I did some benchmarking: library(microbenchmark) x <- 1:5 vec <- c(2,4,3) fastWolfgang <- function(v, vec) matrix(c(v, rep(vec, each = length(v))), nrow = length(v)) microbenchmark(cbind(x, t(replicate(length(x), vec))), t(sapply(x, function(x) c(x, vec))), do.call(rbind, lapply(x, function(x) c(x, vec))), t(mapply(c, x, MoreArgs=list(vec))), Reduce(cbind, vec, x), Reduce(cbind2, vec, x), fastWolfgang(x, vec), times=1L) Jeff's approach is fastest, but Gabor's Reduce(cbind, vec, x) is close (and I really like its simplicity); and very similar to the do.call() approach. Interestingly, for larger vectors, such as: x <- 1:50 vec <- sample(1:100, 200, replace=TRUE) the do.call() approach is the fastest. Best, Wolfgang >-Original Message- >From: Jeff Newmiller [mailto:jdnew...@dcn.davis.ca.us] >Sent: Tuesday, 03 July, 2018 17:48 >To: r-help@r-project.org; Viechtbauer, Wolfgang (SP); r-help@r- >project.org >Subject: Re: [R] Combine by columns a vector with another vector that is >constant across rows > >Sorry trying again... > >fastWolfgang <- function( v, vec ) { > matrix( c( v, rep( vec, each = length( v ) ) ) > , nrow = length( v ) ) >} > >On July 3, 2018 8:21:47 AM PDT, Jeff Newmiller >wrote: >>Gabor's solution seems to optimize 'simpler'. >> >>More efficient is to learn that in R a vector is not a matrix, but a >>matrix is just an ornamented vector. >> >>fastWolfgang <- function( v, vec ) { >> matrix( c( v, rep( vec, length( v ) ) ) >> , now = length( v ) ) >>} >> >>On July 3, 2018 6:28:45 AM PDT, "Viechtbauer, Wolfgang (SP)" >> wrote: >>>Hi All, >>> >>>I have one vector that I want to combine with another vector and that >>>other vector should be the same for every row in the combined matrix. >>>This obviously does not work: >>> >>>vec <- c(2,4,3) >>>cbind(1:5, vec) >>> >>>This does, but requires me to specify the correct value for 'n' in >>>replicate(): >>> >>>cbind(1:5, t(replicate(5, vec))) >>> >>>Other ways that do not require this are: >>> >>>t(sapply(1:5, function(x) c(x, vec))) >>>do.call(rbind, lapply(1:5, function(x) c(x, vec))) >>>t(mapply(c, 1:5, MoreArgs=list(vec))) >>> >>>I wonder if there is a simpler / more efficient way of doing this. >>> >>>Best, >>>Wolfgang __ 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] Combine by columns a vector with another vector that is constant across rows
Sorry trying again... fastWolfgang <- function( v, vec ) { matrix( c( v, rep( vec, each = length( v ) ) ) , nrow = length( v ) ) } On July 3, 2018 8:21:47 AM PDT, Jeff Newmiller wrote: >Gabor's solution seems to optimize 'simpler'. > >More efficient is to learn that in R a vector is not a matrix, but a >matrix is just an ornamented vector. > >fastWolfgang <- function( v, vec ) { > matrix( c( v, rep( vec, length( v ) ) ) > , now = length( v ) ) >} > >On July 3, 2018 6:28:45 AM PDT, "Viechtbauer, Wolfgang (SP)" > wrote: >>Hi All, >> >>I have one vector that I want to combine with another vector and that >>other vector should be the same for every row in the combined matrix. >>This obviously does not work: >> >>vec <- c(2,4,3) >>cbind(1:5, vec) >> >>This does, but requires me to specify the correct value for 'n' in >>replicate(): >> >>cbind(1:5, t(replicate(5, vec))) >> >>Other ways that do not require this are: >> >>t(sapply(1:5, function(x) c(x, vec))) >>do.call(rbind, lapply(1:5, function(x) c(x, vec))) >>t(mapply(c, 1:5, MoreArgs=list(vec))) >> >>I wonder if there is a simpler / more efficient way of doing this. >> >>Best, >>Wolfgang >> >>__ >>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. -- Sent from my phone. Please excuse my brevity. __ 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] Combine by columns a vector with another vector that is constant across rows
or this variation if you don't want the first column to be named init: Reduce(cbind2, vec, 1:5) On Tue, Jul 3, 2018 at 10:46 AM, Gabor Grothendieck wrote: > Try Reduce: > > Reduce(cbind, vec, 1:5) > > On Tue, Jul 3, 2018 at 9:28 AM, Viechtbauer, Wolfgang (SP) > wrote: >> Hi All, >> >> I have one vector that I want to combine with another vector and that other >> vector should be the same for every row in the combined matrix. This >> obviously does not work: >> >> vec <- c(2,4,3) >> cbind(1:5, vec) >> >> This does, but requires me to specify the correct value for 'n' in >> replicate(): >> >> cbind(1:5, t(replicate(5, vec))) >> >> Other ways that do not require this are: >> >> t(sapply(1:5, function(x) c(x, vec))) >> do.call(rbind, lapply(1:5, function(x) c(x, vec))) >> t(mapply(c, 1:5, MoreArgs=list(vec))) >> >> I wonder if there is a simpler / more efficient way of doing this. >> >> Best, >> Wolfgang >> >> __ >> 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. > > > > -- > Statistics & Software Consulting > GKX Group, GKX Associates Inc. > tel: 1-877-GKX-GROUP > email: ggrothendieck at gmail.com -- 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 -- 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] Combine by columns a vector with another vector that is constant across rows
Gabor's solution seems to optimize 'simpler'. More efficient is to learn that in R a vector is not a matrix, but a matrix is just an ornamented vector. fastWolfgang <- function( v, vec ) { matrix( c( v, rep( vec, length( v ) ) ) , now = length( v ) ) } On July 3, 2018 6:28:45 AM PDT, "Viechtbauer, Wolfgang (SP)" wrote: >Hi All, > >I have one vector that I want to combine with another vector and that >other vector should be the same for every row in the combined matrix. >This obviously does not work: > >vec <- c(2,4,3) >cbind(1:5, vec) > >This does, but requires me to specify the correct value for 'n' in >replicate(): > >cbind(1:5, t(replicate(5, vec))) > >Other ways that do not require this are: > >t(sapply(1:5, function(x) c(x, vec))) >do.call(rbind, lapply(1:5, function(x) c(x, vec))) >t(mapply(c, 1:5, MoreArgs=list(vec))) > >I wonder if there is a simpler / more efficient way of doing this. > >Best, >Wolfgang > >__ >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. -- Sent from my phone. Please excuse my brevity. __ 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] Combine by columns a vector with another vector that is constant across rows
Try Reduce: Reduce(cbind, vec, 1:5) On Tue, Jul 3, 2018 at 9:28 AM, Viechtbauer, Wolfgang (SP) wrote: > Hi All, > > I have one vector that I want to combine with another vector and that other > vector should be the same for every row in the combined matrix. This > obviously does not work: > > vec <- c(2,4,3) > cbind(1:5, vec) > > This does, but requires me to specify the correct value for 'n' in > replicate(): > > cbind(1:5, t(replicate(5, vec))) > > Other ways that do not require this are: > > t(sapply(1:5, function(x) c(x, vec))) > do.call(rbind, lapply(1:5, function(x) c(x, vec))) > t(mapply(c, 1:5, MoreArgs=list(vec))) > > I wonder if there is a simpler / more efficient way of doing this. > > Best, > Wolfgang > > __ > 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. -- 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 -- 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] Combine by columns a vector with another vector that is constant across rows
Hi If you put 1:5 vector to x you could do cbind(x,t(replicate(length(x), vec))) Cheers Petr Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních partnerů PRECHEZA a.s. jsou zveřejněny na: https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about processing and protection of business partner's personal data are available on website: https://www.precheza.cz/en/personal-data-protection-principles/ Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a podléhají tomuto právně závaznému prohlášení o vyloučení odpovědnosti: https://www.precheza.cz/01-dovetek/ | This email and any documents attached to it may be confidential and are subject to the legally binding disclaimer: https://www.precheza.cz/en/01-disclaimer/ > -Original Message- > From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Viechtbauer, > Wolfgang (SP) > Sent: Tuesday, July 3, 2018 3:29 PM > To: r-help@r-project.org > Subject: [R] Combine by columns a vector with another vector that is constant > across rows > > Hi All, > > I have one vector that I want to combine with another vector and that other > vector should be the same for every row in the combined matrix. This obviously > does not work: > > vec <- c(2,4,3) > cbind(1:5, vec) > > This does, but requires me to specify the correct value for 'n' in > replicate(): > > cbind(1:5, t(replicate(5, vec))) > > Other ways that do not require this are: > > t(sapply(1:5, function(x) c(x, vec))) > do.call(rbind, lapply(1:5, function(x) c(x, vec))) t(mapply(c, 1:5, > MoreArgs=list(vec))) > > I wonder if there is a simpler / more efficient way of doing this. > > Best, > Wolfgang > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Combine by columns a vector with another vector that is constant across rows
Hi All, I have one vector that I want to combine with another vector and that other vector should be the same for every row in the combined matrix. This obviously does not work: vec <- c(2,4,3) cbind(1:5, vec) This does, but requires me to specify the correct value for 'n' in replicate(): cbind(1:5, t(replicate(5, vec))) Other ways that do not require this are: t(sapply(1:5, function(x) c(x, vec))) do.call(rbind, lapply(1:5, function(x) c(x, vec))) t(mapply(c, 1:5, MoreArgs=list(vec))) I wonder if there is a simpler / more efficient way of doing this. Best, Wolfgang __ 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] Combine vectors under the names
Your code is syntactically correct but goes against all R style guides I know. I've changed that - but obviously you don't have to. x1 <- c(a1 = 0, b3 = 2, e2 = -2) x2 <- c(c = 3, d = 4, f = 5) N <- c("a1", "b3", "d", "e2", "c", "f") x3 <- c(x1, x2) # concatenate x3 <- x3[N] # re-order The assumption is that N contains each of names(x1) and names(x2) exactly once. If that isn't guaranteed, a different approach is needed. B. > On Apr 2, 2017, at 5:02 PM, Art Uwrote: > > Hello, > > Lets say I have 2 vectors: > x1=c("a1"=0,"b3"=2,"e2"=-2); > x2=c("c"=3,"d"=4,"f"=5); > and vector of names in specific order: > N=c("a1","b3","d","e2","c","f") > and I want to combine them to vector C: > > C= > > a1 b3 d e2 c f > 0 2 4 -2 3 5 > > > Basically, just fill vector N with values from vector x1 and x2. How can I > do that? > Thank you in advance. > Art > > -- > *I like to pretend I'm alone*. *Completely alone*. *Maybe post-apocalypse > or plague*... *Whatever*. *No-one left to act normal for. No need to hide > who I really am. It would be... freeing*. *...* > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Combine vectors under the names
Hello, Lets say I have 2 vectors: x1=c("a1"=0,"b3"=2,"e2"=-2); x2=c("c"=3,"d"=4,"f"=5); and vector of names in specific order: N=c("a1","b3","d","e2","c","f") and I want to combine them to vector C: C= a1 b3 d e2 c f 0 2 4 -2 3 5 Basically, just fill vector N with values from vector x1 and x2. How can I do that? Thank you in advance. Art -- *I like to pretend I'm alone*. *Completely alone*. *Maybe post-apocalypse or plague*... *Whatever*. *No-one left to act normal for. No need to hide who I really am. It would be... freeing*. *...* [[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] combine
?merge with all.x and all.y both set to TRUE. Use the NA's **NOT** 0's for nonmatching values that merge() gives. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Sat, Mar 25, 2017 at 9:37 PM, Ashtawrote: > Hi all, > > I have more than two files and merge by a single column and preserve the > other columns. > Here is an example of two files > > dat1 <- read.table(header=TRUE, text=' ID T1 T2 > ID1125245 > ID2141264 > ID3133281') > > dat2 <- read.table(header=TRUE, text=' ID G1 G2 > ID225 46 > ID4 4164 > ID53381') > > How do I get the following output? > > ID T1 T2 G1G2 > ID11252450 0 > ID2141264 2546 > ID3133281 0 0 > ID4 0 0 41 64 > ID5 0 0 33 81 > > Thank you. > > [[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] combine
Initially: dat3 <- merge(dat1, dat2, all.x = TRUE, all.y = TRUE) ... and then you had asked for 0, not NA in your results. I think that's not a good idea - since you can't distinguish a legitimate value 0 from a missing value that way, but if you must: dat3[is.na(dat3)] <- 0 B. (and don't post in HTML) > On Mar 26, 2017, at 12:37 AM, Ashtawrote: > > Hi all, > > I have more than two files and merge by a single column and preserve the > other columns. > Here is an example of two files > > dat1 <- read.table(header=TRUE, text=' ID T1 T2 > ID1125245 > ID2141264 > ID3133281') > > dat2 <- read.table(header=TRUE, text=' ID G1 G2 > ID225 46 > ID4 4164 > ID53381') > > How do I get the following output? > > ID T1 T2 G1G2 > ID11252450 0 > ID2141264 2546 > ID3133281 0 0 > ID4 0 0 41 64 > ID5 0 0 33 81 > > Thank you. > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] combine
Hi all, I have more than two files and merge by a single column and preserve the other columns. Here is an example of two files dat1 <- read.table(header=TRUE, text=' ID T1 T2 ID1125245 ID2141264 ID3133281') dat2 <- read.table(header=TRUE, text=' ID G1 G2 ID225 46 ID4 4164 ID53381') How do I get the following output? ID T1 T2 G1G2 ID11252450 0 ID2141264 2546 ID3133281 0 0 ID4 0 0 41 64 ID5 0 0 33 81 Thank you. [[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] combine trellis lattice contour plot with simple plot() points() and text() commands?
can I add ordinary graphics commands to a contourplot? my naive attempts are telling me that plot.new() has not yet been called when I try to add text(1,1,hi) or points( c(0,1), c(1,0) )? [or do I need to rewrite another contourplot with the old graphics system. the basics are probably looking at adjacent points, pretending that they are linear, and mark where a line between them intercepts the level, and then hope that some sanity prevents me from connecting disconnected levels. not my plan...] Ivo Welch (ivo.we...@gmail.com) http://www.ivo-welch.info/ __ 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] combine trellis lattice contour plot with simple plot() points() and text() commands?
On Jun 1, 2015, at 2:03 PM, ivo welch wrote: can I add ordinary graphics commands to a contourplot? my naive attempts are telling me that plot.new() has not yet been called when I try to add text(1,1,hi) or points( c(0,1), c(1,0) )? There are lattice equivalents to many of the base primitives. They can be used inside lattice/trellis functions or used later if the plots have been save to names. ?llines ?trellis.focus Also take a look at the lattice Extra and gridBase packages for additional faclities like these. Examples requested for demonstration ... as always but using your earleir example: myplot= contourplot( z ~ x * y, data = d) myplot trellis.focus(panel,1,1) llines(x=3, y=1:5) #draws a blue segment on the screen device NULL trellis.unfocus() But that is not going to change myplot. For that you need ?update.trellis or latticeExtra's version of +. -- david. [or do I need to rewrite another contourplot with the old graphics system. the basics are probably looking at adjacent points, pretending that they are linear, and mark where a line between them intercepts the level, and then hope that some sanity prevents me from connecting disconnected levels. not my plan...] Ivo Welch (ivo.we...@gmail.com) http://www.ivo-welch.info/ David Winsemius Alameda, CA, USA __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] combine trellis lattice contour plot with simple plot() points() and text() commands?
Hi Ivo If you want to add lines, text etc you can do that by a lattice panel function included in it would be panel.contour something like (untested) contourplot(... panel = function(x,y, etc,...){ panel.countorplot(x,y, etc) panel.lines(x,y,...) panel.text() } ) and see also library(lattice) names(trellis.par.get()) and delve into the names that come up that are applicable See the help page ? panel.lines which should cover most of what you need. Remember that you may have to look at ? grid::gpar for fine tuning of the arguments Just finished a plot with panel.segments panel.segments(xlocs - medsbar.len, meds, xlocs + medsbar.len, meds, lineend = 1, # grid gpar call for line ending lwd = 5, col = 2) Duncan Duncan Mackay Department of Agronomy and Soil Science University of New England Armidale NSW 2351 Email: home: mac...@northnet.com.au -Original Message- From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of ivo welch Sent: Tuesday, 2 June 2015 07:03 To: r-help Subject: [R] combine trellis lattice contour plot with simple plot() points() and text() commands? can I add ordinary graphics commands to a contourplot? my naive attempts are telling me that plot.new() has not yet been called when I try to add text(1,1,hi) or points( c(0,1), c(1,0) )? [or do I need to rewrite another contourplot with the old graphics system. the basics are probably looking at adjacent points, pretending that they are linear, and mark where a line between them intercepts the level, and then hope that some sanity prevents me from connecting disconnected levels. not my plan...] Ivo Welch (ivo.we...@gmail.com) http://www.ivo-welch.info/ __ 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] combine trellis lattice contour plot with simple plot() points() and text() commands?
Hi Ivo, Not in a sane way. Have a look at the latticeExtra package that allows most of these things to be done. Jim On Tue, Jun 2, 2015 at 7:03 AM, ivo welch ivo.we...@anderson.ucla.edu wrote: can I add ordinary graphics commands to a contourplot? my naive attempts are telling me that plot.new() has not yet been called when I try to add text(1,1,hi) or points( c(0,1), c(1,0) )? [or do I need to rewrite another contourplot with the old graphics system. the basics are probably looking at adjacent points, pretending that they are linear, and mark where a line between them intercepts the level, and then hope that some sanity prevents me from connecting disconnected levels. not my plan...] Ivo Welch (ivo.we...@gmail.com) http://www.ivo-welch.info/ __ 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] Combine list element by column name to make a dataframe
forgot to cc to list have a look at https://stat.ethz.ch/pipermail/r-help/2012-January/300275.html and other messages in the sequence if you use Marc Schwartz's list2df you with have to transpose it with t() Duncan Duncan Mackay Department of Agronomy and Soil Science University of New England Armidale NSW 2351 Email: home: mac...@northnet.com.au -Original Message- From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Mohammad Tanvir Ahamed via R-help Sent: Monday, 6 April 2015 16:06 To: r-help@r-project.org Subject: [R] Combine list element by column name to make a dataframe Hi ,� I have a example list like follow� lst-list(setNames(c(1,10,50,60,70,80),c(id,id1,math,phy,che,bio)),setNames(c(2,20,45),c(id,id1,phy)),setNames(c(3,30,75),c(id,id1,bio))) My expected outcome :� - df-rbind(c(1,10,50,60,70,80),c(2,20,NA,45,NA,NA),c(3,30,NA,NA,NA,75)) colnames(df)-c(id,id1,math,phy,che,bio) row.names(df) - NULL df Any suggestion will be appreciated .� Thanks in advance. � Best regards ...� Tanvir Ahamed G�teborg, Sweden [[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] Combine list element by column name to make a dataframe
Maybe something like the appended? -- Mike lst - list(setNames(c(1,10,50,60,70,80), c(id,id1,math,phy,che,bio)), setNames(c(2,20,45), c(id,id1,phy)), setNames(c(3,30,75), c(id,id1,bio))) lst df - rbind(c(1,10,50,60,70,80), c(2,20,NA,45,NA,NA), c(3,30,NA,NA,NA,75)) colnames(df)-c(id,id1,math,phy,che,bio) row.names(df) - NULL df allNames - unique(unlist(lapply(lst, names))) allNames newLst - lapply(lst, function(element) { element[allNames] }) newLst df2 - do.call(rbind, newLst) df2 all.equal(df, df2) On Sun, Apr 5, 2015 at 11:05 PM, Mohammad Tanvir Ahamed via R-help r-help@r-project.org wrote: Hi , I have a example list like follow lst-list(setNames(c(1,10,50,60,70,80),c(id,id1,math,phy,che,bio)),setNames(c(2,20,45),c(id,id1,phy)),setNames(c(3,30,75),c(id,id1,bio))) My expected outcome : - df-rbind(c(1,10,50,60,70,80),c(2,20,NA,45,NA,NA),c(3,30,NA,NA,NA,75)) colnames(df)-c(id,id1,math,phy,che,bio) row.names(df) - NULL df Any suggestion will be appreciated . Thanks in advance. Best regards ... Tanvir Ahamed Göteborg, Sweden [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Combine list element by column name to make a dataframe
Hi ,� I have a example list like follow� lst-list(setNames(c(1,10,50,60,70,80),c(id,id1,math,phy,che,bio)),setNames(c(2,20,45),c(id,id1,phy)),setNames(c(3,30,75),c(id,id1,bio))) My expected outcome :� - df-rbind(c(1,10,50,60,70,80),c(2,20,NA,45,NA,NA),c(3,30,NA,NA,NA,75)) colnames(df)-c(id,id1,math,phy,che,bio) row.names(df) - NULL df Any suggestion will be appreciated .� Thanks in advance. � Best regards ...� Tanvir Ahamed G�teborg, Sweden [[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] Combine list element by column name to make a dataframe
On 06 Apr 2015, at 08:05 , Mohammad Tanvir Ahamed via R-help r-help@r-project.org wrote: Hi ,� I have a example list like follow� lst-list(setNames(c(1,10,50,60,70,80),c(id,id1,math,phy,che,bio)),setNames(c(2,20,45),c(id,id1,phy)),setNames(c(3,30,75),c(id,id1,bio))) My expected outcome :� - df-rbind(c(1,10,50,60,70,80),c(2,20,NA,45,NA,NA),c(3,30,NA,NA,NA,75)) colnames(df)-c(id,id1,math,phy,che,bio) row.names(df) - NULL df Any suggestion will be appreciated .� Hmm, in principle this looks like a merge() problem, if you first convert each list element to a data frame. That could be painful to get right though. You could try something like this: nm - Reduce(union, lapply(lst, names)) # or just type it in nm [1] id id1 math phy che bio blank - setNames(rep(NA_real_, length(nm)), nm) fill1 - function(x, blank) {blank[names(x)] - x; blank} lapply(lst, fill1, blank) [[1]] id id1 math phy che bio 1 10 50 60 70 80 [[2]] id id1 math phy che bio 2 20 NA 45 NA NA [[3]] id id1 math phy che bio 3 30 NA NA NA 75 do.call(rbind,lapply(lst, fill1, blank)) id id1 math phy che bio [1,] 1 10 50 60 70 80 [2,] 2 20 NA 45 NA NA [3,] 3 30 NA NA NA 75 (NB, this is a matrix, not a data frame, but so is your df!) Notice that this does not do a proper merge on the id fields, i.e. if you have two different records with different grades (say one with che and another with phy) on the same id, you get two records, not one. However, that might well be what you wanted. (It is tempting to use fill1 - function(x) `[-`(blank, names(x), x) lapply(lst, fill1) which does seem to work, but should probably be avoided because of the risk of destructive modification.) Thanks in advance. � Best regards ...� Tanvir Ahamed G�teborg, Sweden [[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. -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd@cbs.dk Priv: pda...@gmail.com __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Combine factorial column intp a new column
Dear R-users, I'd be very greatful if you could help me with the following as after a few tests I haven't still been able to get the right outcome. I've got this data: dd_1 - data.frame(ID = c(1,2, 3, 4, 5), Class_a = c(a,NA, a, NA, NA), Class_b = c(NA, b, b, b, b)) And I'd like to produce a new column 'CLASS': dd_2 - data.frame(ID = c(1,2, 3, 4, 5), Class_a = c(a,NA, a, NA, NA), Class_b = c(NA, b, b, b, b), CLASS = c(a, b, a-b, b, b)) Thanks a lot! __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Combine factorial column intp a new column
Hi, May be this helps: ddNew - transform(dd_1, CLASS= gsub(NA-|-NA,,paste(Class_a, Class_b, sep=-))) identical(ddNew, dd_2) #[1] TRUE A.K. On Thursday, May 22, 2014 4:07 AM, Beatriz R. Gonzalez Dominguez aguitatie...@hotmail.com wrote: Dear R-users, I'd be very greatful if you could help me with the following as after a few tests I haven't still been able to get the right outcome. I've got this data: dd_1 - data.frame(ID = c(1,2, 3, 4, 5), Class_a = c(a,NA, a, NA, NA), Class_b = c(NA, b, b, b, b)) And I'd like to produce a new column 'CLASS': dd_2 - data.frame(ID = c(1,2, 3, 4, 5), Class_a = c(a,NA, a, NA, NA), Class_b = c(NA, b, b, b, b), CLASS = c(a, b, a-b, b, b)) Thanks a lot! __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] combine 2 data.frames in dependence of the ID
thanks for the help up to here. A little problem remains. I have different ABNR, if i try it with another ABNR, the Column extend for each ABNR, it should start with FIRST again. dat1 - read.table(text=FS_ID ABNR 932733688812 11391 33688812 11392 33688812 11388 33688812 11390 33688812 12028 33688812 12029 33688812 1 3380,sep=,header=TRUE) dat2 - read.table(text=FS_ID DATE POST 11390 2012-12-1328 12029 2013-01-1728.3 11391 2011-02-2029 1 2014-02-2010,header=TRUE,stringsAsFactors=FALSE) library(reshape2) setNames(dcast(merge(dat1,dat2, by=FS_ID)[,-1],ABNR~DATE,value.var=POST),c(ABNR,FIRST,SECOND)) ABNR FIRST SECOND THREE NA 1 3368881229 28 28.3 NA 2 3380NA NANA 10 it shoult start for each ABNR in the FIRST-Column again. Right would be: ABNR FIRST SECOND THREE 1 3368881229 28 28.3 2 338010 NANA Thank you. -- View this message in context: http://r.789695.n4.nabble.com/combine-2-data-frames-in-dependence-of-the-ID-tp4685781p4685855.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] combine 2 data.frames in dependence of the ID
Hi Mat, Please check the str() of your dat2. If I change dat2$DATE - as.POSIXlt(dat2$DATE) dcast(ddply(merge(dat1,dat2,by=FS_ID)[,-1],.(ABNR), mutate, DATE=seq_along(DATE)),ABNR~DATE,value.var=POST) #Error in attributes(out) - attributes(col) : # 'names' attribute [9] must be the same length as the vector [3] A.K. On Wednesday, February 26, 2014 9:57 AM, Matthias Weber matthias.we...@fntsoftware.com wrote: Thanks first for the help, Your description works with the example perfect. If I try it with my data, there is an error. error in attributes(out) - attributes(col) : attribute 'names' [306] must have the same length, than the vector [1] My data.frame dat2 has 306 lines, could be this the reason? What does R mean with the vector? Thanks a lot for the help. Best regards. Mat. -Ursprüngliche Nachricht- Von: arun [mailto:smartpink...@yahoo.com] Gesendet: Mittwoch, 26. Februar 2014 10:08 An: Matthias Weber Betreff: Re: [R] combine 2 data.frames in dependence of the ID #or you could do: dcast(ddply(merge(dat1,dat2,by=FS_ID)[,-1],.(ABNR), mutate, DATE=cut(seq_along(DATE),breaks=c(0,1,2,3,4,5,6,7,8,9),labels=c(FIRST,SECOND,THREE,FOUR,FIVE,SIX,SEVEN,EIGHT,NINE))),ABNR~DATE,value.var=POST) # ABNR FIRST SECOND THREE #1 33688812 28 29 28.3 #2 3380 10 NA NA A.K. On Wednesday, February 26, 2014 3:58 AM, arun smartpink...@yahoo.com wrote: Try: library(plyr) library(reshape2) res - dcast(ddply(merge(dat1,dat2,by=FS_ID)[,-1],.(ABNR), mutate, DATE=seq_along(DATE)),ABNR~DATE,value.var=POST) colnames(res)[-1] - c(FIRST,SECOND,THREE) res # ABNR FIRST SECOND THREE #1 33688812 28 29 28.3 #2 3380 10 NA NA A.K. On Wednesday, February 26, 2014 3:06 AM, Mat matthias.we...@fnt.de wrote: thanks for the help up to here. A little problem remains. I have different ABNR, if i try it with another ABNR, the Column extend for each ABNR, it should start with FIRST again. dat1 - read.table(text=FS_ID ABNR 9327 33688812 11391 33688812 11392 33688812 11388 33688812 11390 33688812 12028 33688812 12029 33688812 1 3380,sep=,header=TRUE) dat2 - read.table(text=FS_ID DATE POST 11390 2012-12-13 28 12029 2013-01-17 28.3 11391 2011-02-20 29 1 2014-02-20 10,header=TRUE,stringsAsFactors=FALSE) library(reshape2) setNames(dcast(merge(dat1,dat2, by=FS_ID)[,-1],ABNR~DATE,value.var=POST),c(ABNR,FIRST,SECOND)) ABNR FIRST SECOND THREE NA 1 33688812 29 28 28.3 NA 2 3380 NA NA NA 10 it shoult start for each ABNR in the FIRST-Column again. Right would be: ABNR FIRST SECOND THREE 1 33688812 29 28 28.3 2 3380 10 NA NA Thank you. -- View this message in context: http://r.789695.n4.nabble.com/combine-2-data-frames-in-dependence-of-the-ID-tp4685781p4685855.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] combine 2 data.frames in dependence of the ID
Hi Mat, Please check the str() of your dat2. If I change dat2$DATE - as.POSIXlt(dat2$DATE) dcast(ddply(merge(dat1,dat2,by=FS_ID)[,-1],.(ABNR), mutate, DATE=seq_along(DATE)),ABNR~DATE,value.var=POST) #Error in attributes(out) - attributes(col) : # 'names' attribute [9] must be the same length as the vector [3] A.K. On Wednesday, February 26, 2014 9:57 AM, Matthias Weber matthias.we...@fntsoftware.com wrote: Thanks first for the help, Your description works with the example perfect. If I try it with my data, there is an error. error in attributes(out) - attributes(col) : attribute 'names' [306] must have the same length, than the vector [1] My data.frame dat2 has 306 lines, could be this the reason? What does R mean with the vector? Thanks a lot for the help. Best regards. Mat. -Ursprüngliche Nachricht- Von: arun [mailto:smartpink...@yahoo.com] Gesendet: Mittwoch, 26. Februar 2014 10:08 An: Matthias Weber Betreff: Re: [R] combine 2 data.frames in dependence of the ID #or you could do: dcast(ddply(merge(dat1,dat2,by=FS_ID)[,-1],.(ABNR), mutate, DATE=cut(seq_along(DATE),breaks=c(0,1,2,3,4,5,6,7,8,9),labels=c(FIRST,SECOND,THREE,FOUR,FIVE,SIX,SEVEN,EIGHT,NINE))),ABNR~DATE,value.var=POST) # ABNR FIRST SECOND THREE #1 33688812 28 29 28.3 #2 3380 10 NA NA A.K. On Wednesday, February 26, 2014 3:58 AM, arun smartpink...@yahoo.com wrote: Try: library(plyr) library(reshape2) res - dcast(ddply(merge(dat1,dat2,by=FS_ID)[,-1],.(ABNR), mutate, DATE=seq_along(DATE)),ABNR~DATE,value.var=POST) colnames(res)[-1] - c(FIRST,SECOND,THREE) res # ABNR FIRST SECOND THREE #1 33688812 28 29 28.3 #2 3380 10 NA NA A.K. On Wednesday, February 26, 2014 3:06 AM, Mat matthias.we...@fnt.de wrote: thanks for the help up to here. A little problem remains. I have different ABNR, if i try it with another ABNR, the Column extend for each ABNR, it should start with FIRST again. dat1 - read.table(text=FS_ID ABNR 9327 33688812 11391 33688812 11392 33688812 11388 33688812 11390 33688812 12028 33688812 12029 33688812 1 3380,sep=,header=TRUE) dat2 - read.table(text=FS_ID DATE POST 11390 2012-12-13 28 12029 2013-01-17 28.3 11391 2011-02-20 29 1 2014-02-20 10,header=TRUE,stringsAsFactors=FALSE) library(reshape2) setNames(dcast(merge(dat1,dat2, by=FS_ID)[,-1],ABNR~DATE,value.var=POST),c(ABNR,FIRST,SECOND)) ABNR FIRST SECOND THREE NA 1 33688812 29 28 28.3 NA 2 3380 NA NA NA 10 it shoult start for each ABNR in the FIRST-Column again. Right would be: ABNR FIRST SECOND THREE 1 33688812 29 28 28.3 2 3380 10 NA NA Thank you. -- View this message in context: http://r.789695.n4.nabble.com/combine-2-data-frames-in-dependence-of-the-ID-tp4685781p4685855.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] combine 2 data.frames in dependence of the ID
Hello together, i have a little problem, maybe anyone can help me. I have 2 data.frame, one look like this one: FS_ID ABNR 9327 33688812 11391 33688812 11392 33688812 11388 33688812 11390 33688812 12028 33688812 12029 33688812 the other data.frame looks like as follows: FS_ID DATE POST 11390 2012-12-13 28 12029 2013-01-17 28.3 what i am looking for, is a result, which look like this one: ABNR FIRST SECOND 33688812 28 28.3 The ABNR and the POST-Value of my second data.frame should stand in one row. The earlier date value should stand under FIRST, the newer date under SECOND. Maybe anyone can help me, how i can do this. Best regards. Mat -- View this message in context: http://r.789695.n4.nabble.com/combine-2-data-frames-in-dependence-of-the-ID-tp4685781.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] combine 2 data.frames in dependence of the ID
Hi, May be this helps: dat1 - read.table(text=FS_ID ABNR 9327 33688812 11391 33688812 11392 33688812 11388 33688812 11390 33688812 12028 33688812 12029 33688812,sep=,header=TRUE) dat2 - read.table(text=FS_ID DATE POST 11390 2012-12-13 28 12029 2013-01-17 28.3,header=TRUE,stringsAsFactors=FALSE) library(reshape2) setNames(dcast(merge(dat1,dat2, by=FS_ID)[,-1],ABNR~DATE,value.var=POST),c(ABNR,FIRST,SECOND)) # ABNR FIRST SECOND #1 33688812 28 28.3 A.K. On Tuesday, February 25, 2014 4:41 AM, Mat matthias.we...@fnt.de wrote: Hello together, i have a little problem, maybe anyone can help me. I have 2 data.frame, one look like this one: FS_ID ABNR 9327 33688812 11391 33688812 11392 33688812 11388 33688812 11390 33688812 12028 33688812 12029 33688812 the other data.frame looks like as follows: FS_ID DATE POST 11390 2012-12-13 28 12029 2013-01-17 28.3 what i am looking for, is a result, which look like this one: ABNR FIRST SECOND 33688812 28 28.3 The ABNR and the POST-Value of my second data.frame should stand in one row. The earlier date value should stand under FIRST, the newer date under SECOND. Maybe anyone can help me, how i can do this. Best regards. Mat -- View this message in context: http://r.789695.n4.nabble.com/combine-2-data-frames-in-dependence-of-the-ID-tp4685781.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] combine glmnet and coxph (and survfit) with strata()
I'm also curious how to use glmnet with survfit -- specifically, for use with interval regression (which, under the hood, is implemented using survfit). Can you show how you converted your Surv object formula to a design matrix for use with glmnet? Thanks, -Aaron On Sun, Dec 8, 2013 at 12:45 AM, Jieyue Li jieyuel...@gmail.com wrote: Dear All, I want to generate survival curve with cox model but I want to estimate the coefficients using glmnet. However, I also want to include a strata() term in the model. Could anyone please tell me how to have this strata() effect in the model in glmnet? I tried converting a formula with strata() to a design matrix and feeding to glmnet, but glmnet just treats the strata() term with one independent variable... I know that if there is no such strata(), I can estimate coefficients from glmnet and use ...init=selectedBeta,iter=0) in the coxph. Please advise me or also correct me if I'm wrong. Thank you very much! Best, Jieyue [[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.
Re: [R] combine glmnet and coxph (and survfit) with strata()
check this one http://stackoverflow.com/questions/16299891/estimating-many-interaction-terms-in-glmnet On Mon, Dec 9, 2013 at 5:39 AM, Aaron Mackey ajmac...@gmail.com wrote: I'm also curious how to use glmnet with survfit -- specifically, for use with interval regression (which, under the hood, is implemented using survfit). Can you show how you converted your Surv object formula to a design matrix for use with glmnet? Thanks, -Aaron On Sun, Dec 8, 2013 at 12:45 AM, Jieyue Li jieyuel...@gmail.com wrote: Dear All, I want to generate survival curve with cox model but I want to estimate the coefficients using glmnet. However, I also want to include a strata() term in the model. Could anyone please tell me how to have this strata() effect in the model in glmnet? I tried converting a formula with strata() to a design matrix and feeding to glmnet, but glmnet just treats the strata() term with one independent variable... I know that if there is no such strata(), I can estimate coefficients from glmnet and use ...init=selectedBeta,iter=0) in the coxph. Please advise me or also correct me if I'm wrong. Thank you very much! Best, Jieyue [[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] combine glmnet and coxph (and survfit) with strata()
Dear All, I want to generate survival curve with cox model but I want to estimate the coefficients using glmnet. However, I also want to include a strata() term in the model. Could anyone please tell me how to have this strata() effect in the model in glmnet? I tried converting a formula with strata() to a design matrix and feeding to glmnet, but glmnet just treats the strata() term with one independent variable... I know that if there is no such strata(), I can estimate coefficients from glmnet and use ...init=selectedBeta,iter=0) in the coxph. Please advise me or also correct me if I'm wrong. Thank you very much! Best, Jieyue [[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] Combine columns having same column name from multiple data frames
Hi, You may try: lapply(seq_len(ncol(data1)),function(i) {x1 - do.call(cbind,lapply(lapply(as.list(paste0(data,1:3)),get),`[`,i)); write.csv(x1,paste0(new,i,.csv),quote=FALSE) }) A.K. Dear All, I am trying to combine columns having same name from 3 different data frames and create new data frame with combined columns. The challenging thing is each data-frame has 100 columns so I have to create 100 new data frames and write each data-frame as new text file with unique column name. Example: data1 1 2 3 4 5 6 7 8 9 x -39532.0 -39472.0 -39472.0 -39592.0 -39532.0 -39472.0 -39412.0 -39592.0 -39412.0 y 2015408.1 2015348.1 2015288.1 2015228.1 2015228.1 2015228.1 2015228.1 2015168.1 2015168.1 id 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 srad_120 496.0 496.0 496.0 496.0 496.0 496.0 496.0 496.0 496.0 srad_121 441.6 441.6 441.6 441.6 441.6 441.6 441.6 441.6 441.6 srad_122 150.4 150.4 150.4 150.4 150.4 150.4 150.4 150.4 150.4 srad_123 249.6 249.6 249.6 249.6 249.6 249.6 249.6 249.6 249.6 srad_124 272.0 272.0 272.0 272.0 272.0 272.0 272.0 272.0 272.0 srad_125 153.6 153.6 153.6 153.6 153.6 153.6 153.6 153.6 153.6 data2 1 2 3 4 5 6 7 8 9 x -39532.0 -39472.0 -39472.0 -39592.0 -39532.0 -39472.0 -39412.0 -39592.0 -39412.0 y 2015408.1 2015348.1 2015288.1 2015228.1 2015228.1 2015228.1 2015228.1 2015168.1 2015168.1 id 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 srad_120 542.0 542.0 542.0 542.0 542.0 542.0 542.0 542.0 542.0 srad_121 487.6 487.6 487.6 487.6 487.6 487.6 487.6 487.6 487.6 srad_122 196.4 196.4 196.4 196.4 196.4 196.4 196.4 196.4 196.4 srad_123 295.6 295.6 295.6 295.6 295.6 295.6 295.6 295.6 295.6 srad_124 318.0 318.0 318.0 318.0 318.0 318.0 318.0 318.0 318.0 srad_125 199.6 199.6 199.6 199.6 199.6 199.6 199.6 199.6 199.6 data3 1 2 3 4 5 6 7 8 9 x -39532.0 -39472.0 -39472.0 -39592.0 -39532.0 -39472.0 -39412.0 -39592.0 -39412.0 y 2015408.1 2015348.1 2015288.1 2015228.1 2015228.1 2015228.1 2015228.1 2015168.1 2015168.1 id 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 srad_120 578.0 578.0 578.0 578.0 578.0 578.0 578.0 578.0 578.0 srad_121 523.6 523.6 523.6 523.6 523.6 523.6 523.6 523.6 523.6 srad_122 232.4 232.4 232.4 232.4 232.4 232.4 232.4 232.4 232.4 srad_123 331.6 331.6 331.6 331.6 331.6 331.6 331.6 331.6 331.6 srad_124 354.0 354.0 354.0 354.0 354.0 354.0 354.0 354.0 354.0 srad_125 235.6 235.6 235.6 235.6 235.6 235.6 235.6 235.6 235.6 write.csv(cbind(data1[,1], data2[,1], data3[,1]), new1.csv) Above statement generates new csv file that has first column of each data frame (data1, data2, data3). V1 V2 V3 x -39531.99524-39531.99524-39531.99524 y 2015408.131 2015408.131 2015408.131 id 1 1 1 srad_120496 542 578 srad_121441.661 487.661 523.661 srad_122150.339 196.339 232.339 srad_123249.661 295.661 331.661 srad_124272 318 354 srad_125153.661 199.661 235.661 Similarly I need to create 9 csv files using 9 columns in each data frame. If possible, please help in this. Thank you very much. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] combine all data frame columns into a vector.
Dear All, Could anyone suggest a quick way to combine all columns in a data frame into a vector? For example, I have a data frame of 205 columns with character data types, many data values are repeated in all the columns. Actually, I would like to retrieve all the unique values from this data set. My strategy is to put all column value into a vector and then select unique from that vector. I would appreciate a more efficient method. Thanks. -Sohail The information contained in this electronic e-mail transmission and any attachments are intended only for the use of the individual or entity to whom or to which it is addressed, and may contain information that is privileged, confidential and exempt from disclosure under applicable law. If the reader of this communication is not the intended recipient, or the employee or agent responsible for delivering this communication to the intended recipient, you are hereby notified that any dissemination, distribution, copying or disclosure of this communication and any attachment is strictly prohibited. If you have received this transmission in error, please notify the sender immediately by telephone and electronic mail, and delete the original communication and any attachment from any computer, server or other electronic recording or storage device or medium. Receipt by anyone other than the intended recipient is not a waiver of any attorney-client, physician-patient or other priv! ilege. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] combine all data frame columns into a vector.
Sohail: 1. Are they character or factor? 2. ?unlist unique(unlist(yourframe)) -- Bert On Mon, Aug 12, 2013 at 1:23 PM, Khan, Sohail skha...@nshs.edu wrote: Dear All, Could anyone suggest a quick way to combine all columns in a data frame into a vector? For example, I have a data frame of 205 columns with character data types, many data values are repeated in all the columns. Actually, I would like to retrieve all the unique values from this data set. My strategy is to put all column value into a vector and then select unique from that vector. I would appreciate a more efficient method. Thanks. -Sohail The information contained in this electronic e-mail transmission and any attachments are intended only for the use of the individual or entity to whom or to which it is addressed, and may contain information that is privileged, confidential and exempt from disclosure under applicable law. If the reader of this communication is not the intended recipient, or the employee or agent responsible for delivering this communication to the intended recipient, you are hereby notified that any dissemination, distribution, copying or disclosure of this communication and any attachment is strictly prohibited. If you have received this transmission in error, please notify the sender immediately by telephone and electronic mail, and delete the original communication and any attachment from any computer, server or other electronic recording or storage device or medium. Receipt by anyone other than the intended recipient is not a waiver of any attorney-client, physician-patient or other pr! iv! ilege. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.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] combine all data frame columns into a vector.
Thanks Bert, All are character values. -Sohail -Original Message- From: Bert Gunter [mailto:gunter.ber...@gene.com] Sent: Monday, August 12, 2013 4:35 PM To: Khan, Sohail Cc: greatest.possible.newbie; r-help@r-project.org Subject: Re: [R] combine all data frame columns into a vector. Sohail: 1. Are they character or factor? 2. ?unlist unique(unlist(yourframe)) -- Bert On Mon, Aug 12, 2013 at 1:23 PM, Khan, Sohail skha...@nshs.edu wrote: Dear All, Could anyone suggest a quick way to combine all columns in a data frame into a vector? For example, I have a data frame of 205 columns with character data types, many data values are repeated in all the columns. Actually, I would like to retrieve all the unique values from this data set. My strategy is to put all column value into a vector and then select unique from that vector. I would appreciate a more efficient method. Thanks. -Sohail The information contained in this electronic e-mail transmission and any attachments are intended only for the use of the individual or entity to whom or to which it is addressed, and may contain information that is privileged, confidential and exempt from disclosure under applicable law. If the reader of this communication is not the intended recipient, or the employee or agent responsible for delivering this communication to the intended recipient, you are hereby notified that any dissemination, distribution, copying or disclosure of this communication and any attachment is strictly prohibited. If you have received this transmission in error, please notify the sender immediately by telephone and electronic mail, and delete the original communication and any attachment from any computer, server or other electronic recording or storage device or medium. Receipt by anyone other than the intended recipient is not a waiver of any attorney-client, physician-patient or other pr! iv! ilege. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm The information contained in this electronic e-mail transmission and any attachments are intended only for the use of the individual or entity to whom or to which it is addressed, and may contain information that is privileged, confidential and exempt from disclosure under applicable law. If the reader of this communication is not the intended recipient, or the employee or agent responsible for delivering this communication to the intended recipient, you are hereby notified that any dissemination, distribution, copying or disclosure of this communication and any attachment is strictly prohibited. If you have received this transmission in error, please notify the sender immediately by telephone and electronic mail, and delete the original communication and any attachment from any computer, server or other electronic recording or storage device or medium. Receipt by anyone other than the intended recipient is not a waiver of any attorney-client, physician-patient or other priv! ilege. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] combine all data frame columns into a vector.
Hi, May be this help: dat1- structure(list(V1 = c(h, f, s, n, r, x, h, t, u, g), V2 = c(p, j, r, r, i, x, f, b, n, d), V3 = c(c, o, s, d, f, r, b, p, q, b ), V4 = c(i, g, j, d, y, f, s, q, s, z), V5 = c(m, j, h, f, b, b, k, j, g, i), V6 = c(m, w, m, s, o, z, l, h, e, d), V7 = c(m, g, h, d, s, i, y, z, t, m), V8 = c(d, f, a, z, q, i, o, v, a, s), V9 = c(n, d, n, f, j, j, g, w, k, v), V10 = c(i, t, y, c, m, p, q, c, k, m)), .Names = c(V1, V2, V3, V4, V5, V6, V7, V8, V9, V10), row.names = c(NA, -10L), class = data.frame) unique(unlist(dat1)) # [1] h f s n r x t u g p j i b d c o q y z #[20] m k w l e a v #or unique(as.vector(as.matrix(dat1))) # [1] h f s n r x t u g p j i b d c o q y z #[20] m k w l e a v A.K. - Original Message - From: Khan, Sohail skha...@nshs.edu To: 'greatest.possible.newbie' daniel.h...@gmx.net; r-help@r-project.org r-help@r-project.org Cc: Sent: Monday, August 12, 2013 4:23 PM Subject: [R] combine all data frame columns into a vector. Dear All, Could anyone suggest a quick way to combine all columns in a data frame into a vector? For example, I have a data frame of 205 columns with character data types, many data values are repeated in all the columns. Actually, I would like to retrieve all the unique values from this data set. My strategy is to put all column value into a vector and then select unique from that vector. I would appreciate a more efficient method. Thanks. -Sohail The information contained in this electronic e-mail transmission and any attachments are intended only for the use of the individual or entity to whom or to which it is addressed, and may contain information that is privileged, confidential and exempt from disclosure under applicable law. If the reader of this communication is not the intended recipient, or the employee or agent responsible for delivering this communication to the intended recipient, you are hereby notified that any dissemination, distribution, copying or disclosure of this communication and any attachment is strictly prohibited. If you have received this transmission in error, please notify the sender immediately by telephone and electronic mail, and delete the original communication and any attachment from any computer, server or other electronic recording or storage device or medium. Receipt by anyone other than the intended recipient is not a waiver of any attorney-client, physician-patient or other priv! ilege. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] combine all data frame columns into a vector.
Thanks Arun and Bert. Both options work. -Sohail -Original Message- From: arun [mailto:smartpink...@yahoo.com] Sent: Monday, August 12, 2013 4:51 PM To: Khan, Sohail Cc: R help Subject: Re: [R] combine all data frame columns into a vector. Hi, May be this help: dat1- structure(list(V1 = c(h, f, s, n, r, x, h, t, u, g), V2 = c(p, j, r, r, i, x, f, b, n, d), V3 = c(c, o, s, d, f, r, b, p, q, b ), V4 = c(i, g, j, d, y, f, s, q, s, z), V5 = c(m, j, h, f, b, b, k, j, g, i), V6 = c(m, w, m, s, o, z, l, h, e, d), V7 = c(m, g, h, d, s, i, y, z, t, m), V8 = c(d, f, a, z, q, i, o, v, a, s), V9 = c(n, d, n, f, j, j, g, w, k, v), V10 = c(i, t, y, c, m, p, q, c, k, m)), .Names = c(V1, V2, V3, V4, V5, V6, V7, V8, V9, V10), row.names = c(NA, -10L), class = data.frame) unique(unlist(dat1)) # [1] h f s n r x t u g p j i b d c o q y z #[20] m k w l e a v #or unique(as.vector(as.matrix(dat1))) # [1] h f s n r x t u g p j i b d c o q y z #[20] m k w l e a v A.K. - Original Message - From: Khan, Sohail skha...@nshs.edu To: 'greatest.possible.newbie' daniel.h...@gmx.net; r-help@r-project.org r-help@r-project.org Cc: Sent: Monday, August 12, 2013 4:23 PM Subject: [R] combine all data frame columns into a vector. Dear All, Could anyone suggest a quick way to combine all columns in a data frame into a vector? For example, I have a data frame of 205 columns with character data types, many data values are repeated in all the columns. Actually, I would like to retrieve all the unique values from this data set. My strategy is to put all column value into a vector and then select unique from that vector. I would appreciate a more efficient method. Thanks. -Sohail The information contained in this electronic e-mail transmission and any attachments are intended only for the use of the individual or entity to whom or to which it is addressed, and may contain information that is privileged, confidential and exempt from disclosure under applicable law. If the reader of this communication is not the intended recipient, or the employee or agent responsible for delivering this communication to the intended recipient, you are hereby notified that any dissemination, distribution, copying or disclosure of this communication and any attachment is strictly prohibited. If you have received this transmission in error, please notify the sender immediately by telephone and electronic mail, and delete the original communication and any attachment from any computer, server or other electronic recording or storage device or medium. Receipt by anyone other than the intended recipient is not a waiver of any attorney-client, physician-patient or other priv! ilege. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. The information contained in this electronic e-mail transmission and any attachments are intended only for the use of the individual or entity to whom or to which it is addressed, and may contain information that is privileged, confidential and exempt from disclosure under applicable law. If the reader of this communication is not the intended recipient, or the employee or agent responsible for delivering this communication to the intended recipient, you are hereby notified that any dissemination, distribution, copying or disclosure of this communication and any attachment is strictly prohibited. If you have received this transmission in error, please notify the sender immediately by telephone and electronic mail, and delete the original communication and any attachment from any computer, server or other electronic recording or storage device or medium. Receipt by anyone other than the intended recipient is not a waiver of any attorney-client, physician-patient or other privilege. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Combine multiple random forests contained in a list
HI, Using the example in ?combine library(randomForest) rf1 - randomForest(Species ~ ., iris, ntree=50, norm.votes=FALSE) rf2 - randomForest(Species ~ ., iris, ntree=50, norm.votes=FALSE) rf3 - randomForest(Species ~ ., iris, ntree=50, norm.votes=FALSE) rf.all - combine(rf1, rf2, rf3) lst1- list(rf1,rf2,rf3) rf.allL- do.call(`combine`,lst1) #or rf.allL- Reduce(`combine,lst1) identical(rf.all,rf.allL) #[1] TRUE A.K. Is there a quick and easy way to pass randomForest objects contained in a list into the combine() function? As a result of calling randomForest through lapply(), I now have 10 randomForests in a list (rfors) I want to combine all 10 of them. Understandably combine(rfors) doesn't work as it doesn't recognise the individual forests within the list. I have spend quite sometime messing around with unlist(), lapply() and apply() to try and extract the information in a suitable format but to no avail. The only thing that works is combine(rfors[[1]], rfors[[2]] ...etc). This is a bit cumbersome though, not least because the number of random forests I'll need to combine is likely to change. Any sleek and elegant solution to this someone can suggest? Thanks in advance for any help. 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] Combine multiple random forests contained in a list
I would say that the use of Reduce in this context is bad practice. from ?Reduce : Reduce uses a binary function to successively combine the elements of a given vector and a possibly given initial value. combine() is obviously not a binary function. do.call() seems to be THE appropriate idiom. -- Bert On Fri, Jul 26, 2013 at 9:26 PM, arun smartpink...@yahoo.com wrote: HI, Using the example in ?combine library(randomForest) rf1 - randomForest(Species ~ ., iris, ntree=50, norm.votes=FALSE) rf2 - randomForest(Species ~ ., iris, ntree=50, norm.votes=FALSE) rf3 - randomForest(Species ~ ., iris, ntree=50, norm.votes=FALSE) rf.all - combine(rf1, rf2, rf3) lst1- list(rf1,rf2,rf3) rf.allL- do.call(`combine`,lst1) #or rf.allL- Reduce(`combine,lst1) identical(rf.all,rf.allL) #[1] TRUE A.K. Is there a quick and easy way to pass randomForest objects contained in a list into the combine() function? As a result of calling randomForest through lapply(), I now have 10 randomForests in a list (rfors) I want to combine all 10 of them. Understandably combine(rfors) doesn't work as it doesn't recognise the individual forests within the list. I have spend quite sometime messing around with unlist(), lapply() and apply() to try and extract the information in a suitable format but to no avail. The only thing that works is combine(rfors[[1]], rfors[[2]] ...etc). This is a bit cumbersome though, not least because the number of random forests I'll need to combine is likely to change. Any sleek and elegant solution to this someone can suggest? Thanks in advance for any help. 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. -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.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] combine select data from 2 dataframes sharing same variables
Hi, Peter. Thanks for the suggestion. I will investigate ?merge. BNC From: Peter Alspach-2 [via R] [mailto:ml-node+s789695n4671809...@n4.nabble.com] Sent: Wednesday, July 17, 2013 8:08 PM To: Crombie, Burnette N Subject: Re: combine select data from 2 dataframes sharing same variables Tena koe Without reading your request in detail, I will suggest you look at ?merge. It is often the answer when 'combine' is in the question. Peter Alspach -- View this message in context: http://r.789695.n4.nabble.com/combine-select-data-from-2-dataframes-sharing-same-variables-tp4671790p4671837.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] combine select data from 2 dataframes sharing same variables
Wow. That is exactly what I wanted (thread viewers see output below). I think/hope this script will be more user friendly for my needs (RTF final output) than the loop I currently have for LaTex output. Thanks very much A.K. Iâll need to send you a Bundt cake at some point I suppose⦠BNC lst1 $MWtotaleesDue zeroNOzeroYES Mean8.428571 0.9076923 StdError2.496256 0.4117990 Median 7.00 0.000 StdDev 6.604472 3.3200295 Min 1.00 0.000 Max17.00 17.000 NinetyPct 3.593998 0.5332467 NinetyPctLower 4.834573 0.3744456 NinetyPctUpper 12.022570 1.4409390 $OTtotaleesDue zeroNOzeroYES Mean6.60 1.0153846 StdError2.242023 0.4442433 Median 3.00 0.000 StdDev 7.089899 3.5816036 Min 1.00 0.000 Max23.00 23.000 NinetyPct 3.100782 0.5752594 NinetyPctLower 3.499218 0.4401252 NinetyPctUpper 9.700782 1.5906440 $OTtotalBWsDue zeroNOzeroYES Mean559.9440 86.14523 StdError305.7341 51.57520 Median 257.55000.0 StdDev 966.8160 415.81256 Min 15.19000.0 Max3232.9700 3232.97000 NinetyPct 422.8390 66.78575 NinetyPctLower 137.1050 19.35948 NinetyPctUpper 982.7830 152.93098 $TotalBWsFD zeroNOzeroYES Mean693.2973 159.99169 StdError265.0846 69.86036 Median 267.58000.0 StdDev 1026.6682 563.23225 Min 15.19000.0 Max3232.9700 3232.97000 NinetyPct 356.5468 90.46357 NinetyPctLower 336.7505 69.52812 NinetyPctUpper 1049.8442 250.45526 From: arun kirshna [via R] [mailto:ml-node+s789695n4671818...@n4.nabble.com] Sent: Thursday, July 18, 2013 12:24 AM To: Crombie, Burnette N Subject: Re: combine select data from 2 dataframes sharing same variables Hi, Not sure if this is what you wanted: #If columns are arranged in the same order in both data.frames. lst1-lapply(seq_len(ncol(StatsUTAH)),function(i) {x1-cbind(StatsUTAH[,i],sStatsUTAH[,i]);row.names(x1)-row.names(StatsUTAH);colnames(x1)-c(zeroNO,zeroYES);x1}) names(lst1)- colnames(StatsUTAH) A.K. -- View this message in context: http://r.789695.n4.nabble.com/combine-select-data-from-2-dataframes-sharing-same-variables-tp4671790p4671887.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] combine select data from 2 dataframes sharing same variables
# The following dataframes are the result of two analyses performed on the same set of numeric data. # The first analysis involved calculations that did not include zero values: StatsUTAH = data.frame(MWtotaleesDue = c(8.428571,2.496256,7,6.604472,1,17,3.593998,4.834573,12.02257), OTtotaleesDue = c(6.6,2.242023,3,7.089899,1,23,3.100782,3.499218,9.700782), OTtotalBWsDue = c(559.944,305.7341,257.55,966.816,15.19,3232.97,422.839,137.105,982.783), TotalBWsFD = c(693.2973,265.0846,267.58,1026.6682,15.19,3232.97,356.5468,336.7505,1049.8442)) rownames(StatsUTAH)- c(Mean,StdError, Median, StdDev, Min, Max, NinetyPct, NinetyPctLower, NinetyPctUpper) StatsUTAH # The second analysis involved calculations that included zero values: sStatsUTAH = data.frame(MWtotaleesDue = c(0.9076923,0.411799,0,3.3200295,0,17,0.5332467,0.3744456,1.440939), OTtotaleesDue = c(1.0153846,0.4442433,0,3.5816036,0,23,0.5752594,0.4401252,1.590644), OTtotalBWsDue = c(86.14523,51.5752,0,415.81256,0,3232.97,66.78575,19.35948,152.93098), TotalBWsFD = c(159.99169,69.86036,0,563.23225,0,3232.97,90.46357,69.52812,250.45526)) rownames(sStatsUTAH)- c(sMean,sStdError, sMedian, sStdDev, sMin, sMax, sNinetyPct, sNinetyPctLower, sNinetyPctUpper) sStatsUTAH #the rows 1-9 may have different names in each dataframe but are the same corresponding calculation in both. # I need to combine these data so that the OUTPUT is a SEPARATE table (or matrix or whatever) # FOR EACH VARIABLE SHARED BY THE DATAFRAMES that I can place in a word document (which I can handle later with RTF). # This is how I've mapped it out in my head, but need to convert to R language: # StatsUTAH ---data for zeroNO # sStatsUTAH ---data for zeroYES # # Table 1: MWtotaleesDue # colnames(zeroNO, zeroYES) # rownames(Mean,StdError, Median, StdDev, Min, Max, NinetyPct, NinetyPctLower, NinetyPctUpper) # # Table 2: OTtotaleesDue # same colnames rownames as Table 1 # # Table 3: OTtotalBWsDue # same colnames rownames as Table 1 # # Table 4: TotalBWsFD # same colnames rownames as Table 1 #WHAT IS THE BEST WAY TO DO THIS IN R? #While a loop may be more efficient, is there also a good way to create each table separately? #Note: my real dataframes (StatsUTAH,etc) will have a lot more variables than what are listed in this example #so I will probably be picking and choosing which ones I'm interested in creating tables for. -- View this message in context: http://r.789695.n4.nabble.com/combine-select-data-from-2-dataframes-sharing-same-variables-tp4671790.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] combine select data from 2 dataframes sharing same variables
Tena koe Without reading your request in detail, I will suggest you look at ?merge. It is often the answer when 'combine' is in the question. Peter Alspach -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of bcrombie Sent: Thursday, 18 July 2013 8:13 a.m. To: r-help@r-project.org Subject: [R] combine select data from 2 dataframes sharing same variables # The following dataframes are the result of two analyses performed on the same set of numeric data. # The first analysis involved calculations that did not include zero values: StatsUTAH = data.frame(MWtotaleesDue = c(8.428571,2.496256,7,6.604472,1,17,3.593998,4.834573,12.02257), OTtotaleesDue = c(6.6,2.242023,3,7.089899,1,23,3.100782,3.499218,9.700782), OTtotalBWsDue = c(559.944,305.7341,257.55,966.816,15.19,3232.97,422.839,137.105,982.783), TotalBWsFD = c(693.2973,265.0846,267.58,1026.6682,15.19,3232.97,356.5468,336.7505,1049.8442)) rownames(StatsUTAH)- c(Mean,StdError, Median, StdDev, Min, Max, NinetyPct, NinetyPctLower, NinetyPctUpper) StatsUTAH # The second analysis involved calculations that included zero values: sStatsUTAH = data.frame(MWtotaleesDue = c(0.9076923,0.411799,0,3.3200295,0,17,0.5332467,0.3744456,1.440939), OTtotaleesDue = c(1.0153846,0.4442433,0,3.5816036,0,23,0.5752594,0.4401252,1.590644), OTtotalBWsDue = c(86.14523,51.5752,0,415.81256,0,3232.97,66.78575,19.35948,152.93098), TotalBWsFD = c(159.99169,69.86036,0,563.23225,0,3232.97,90.46357,69.52812,250.45526)) rownames(sStatsUTAH)- c(sMean,sStdError, sMedian, sStdDev, sMin, sMax, sNinetyPct, sNinetyPctLower, sNinetyPctUpper) sStatsUTAH #the rows 1-9 may have different names in each dataframe but are the same corresponding calculation in both. # I need to combine these data so that the OUTPUT is a SEPARATE table (or matrix or whatever) # FOR EACH VARIABLE SHARED BY THE DATAFRAMES that I can place in a word document (which I can handle later with RTF). # This is how I've mapped it out in my head, but need to convert to R language: # StatsUTAH ---data for zeroNO # sStatsUTAH ---data for zeroYES # # Table 1: MWtotaleesDue # colnames(zeroNO, zeroYES) # rownames(Mean,StdError, Median, StdDev, Min, Max, NinetyPct, NinetyPctLower, NinetyPctUpper) # # Table 2: OTtotaleesDue # same colnames rownames as Table 1 # # Table 3: OTtotalBWsDue # same colnames rownames as Table 1 # # Table 4: TotalBWsFD # same colnames rownames as Table 1 #WHAT IS THE BEST WAY TO DO THIS IN R? #While a loop may be more efficient, is there also a good way to create each table separately? #Note: my real dataframes (StatsUTAH,etc) will have a lot more variables than what are listed in this example #so I will probably be picking and choosing which ones I'm interested in creating tables for. -- View this message in context: http://r.789695.n4.nabble.com/combine-select-data-from-2-dataframes-sharing-same-variables-tp4671790.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. The contents of this e-mail are confidential and may be ...{{dropped:14}} __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] combine select data from 2 dataframes sharing same variables
Hi, Not sure if this is what you wanted: #If columns are arranged in the same order in both data.frames. lst1-lapply(seq_len(ncol(StatsUTAH)),function(i) {x1-cbind(StatsUTAH[,i],sStatsUTAH[,i]);row.names(x1)-row.names(StatsUTAH);colnames(x1)-c(zeroNO,zeroYES);x1}) names(lst1)- colnames(StatsUTAH) A.K. - Original Message - From: bcrombie bcrom...@utk.edu To: r-help@r-project.org Cc: Sent: Wednesday, July 17, 2013 4:12 PM Subject: [R] combine select data from 2 dataframes sharing same variables # The following dataframes are the result of two analyses performed on the same set of numeric data. # The first analysis involved calculations that did not include zero values: StatsUTAH = data.frame(MWtotaleesDue = c(8.428571,2.496256,7,6.604472,1,17,3.593998,4.834573,12.02257), OTtotaleesDue = c(6.6,2.242023,3,7.089899,1,23,3.100782,3.499218,9.700782), OTtotalBWsDue = c(559.944,305.7341,257.55,966.816,15.19,3232.97,422.839,137.105,982.783), TotalBWsFD = c(693.2973,265.0846,267.58,1026.6682,15.19,3232.97,356.5468,336.7505,1049.8442)) rownames(StatsUTAH)- c(Mean,StdError, Median, StdDev, Min, Max, NinetyPct, NinetyPctLower, NinetyPctUpper) StatsUTAH # The second analysis involved calculations that included zero values: sStatsUTAH = data.frame(MWtotaleesDue = c(0.9076923,0.411799,0,3.3200295,0,17,0.5332467,0.3744456,1.440939), OTtotaleesDue = c(1.0153846,0.4442433,0,3.5816036,0,23,0.5752594,0.4401252,1.590644), OTtotalBWsDue = c(86.14523,51.5752,0,415.81256,0,3232.97,66.78575,19.35948,152.93098), TotalBWsFD = c(159.99169,69.86036,0,563.23225,0,3232.97,90.46357,69.52812,250.45526)) rownames(sStatsUTAH)- c(sMean,sStdError, sMedian, sStdDev, sMin, sMax, sNinetyPct, sNinetyPctLower, sNinetyPctUpper) sStatsUTAH #the rows 1-9 may have different names in each dataframe but are the same corresponding calculation in both. # I need to combine these data so that the OUTPUT is a SEPARATE table (or matrix or whatever) # FOR EACH VARIABLE SHARED BY THE DATAFRAMES that I can place in a word document (which I can handle later with RTF). # This is how I've mapped it out in my head, but need to convert to R language: # StatsUTAH ---data for zeroNO # sStatsUTAH ---data for zeroYES # # Table 1: MWtotaleesDue # colnames(zeroNO, zeroYES) # rownames(Mean,StdError, Median, StdDev, Min, Max, NinetyPct, NinetyPctLower, NinetyPctUpper) # # Table 2: OTtotaleesDue # same colnames rownames as Table 1 # # Table 3: OTtotalBWsDue # same colnames rownames as Table 1 # # Table 4: TotalBWsFD # same colnames rownames as Table 1 #WHAT IS THE BEST WAY TO DO THIS IN R? #While a loop may be more efficient, is there also a good way to create each table separately? #Note: my real dataframes (StatsUTAH,etc) will have a lot more variables than what are listed in this example #so I will probably be picking and choosing which ones I'm interested in creating tables for. -- View this message in context: http://r.789695.n4.nabble.com/combine-select-data-from-2-dataframes-sharing-same-variables-tp4671790.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] combine attributes
hello, i have to do a sna for a seminar. i have a csv.data with acteurs. i can identify a cooperation between acteurs with an ID in the csc.data. Is the ID equal with another acteur, so they have an cooperation. furthermore i have an information about the acteurs in the csv.data. there are three types of acteurs in this network: an economy-acteur, univeristy-acteur and techincal college-acteur. Now i want to know, the percentage or the absolute number of the type of cooperation between: university to economy university to technical college university to university and economy to technical college in other words: which type of acteur cooperates with which type of acteur. Can anybody help me? Iam a beginner with this programm and i need a step by step entry / explanation for the correct r-codes. Thanks! -- View this message in context: http://r.789695.n4.nabble.com/combine-attributes-tp4670440.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] combine two columns into one
Hey all! I have a time series dataset like this: DateTime Var 112 1 14 1 1 5 1 2 8 1 2 8 1 2 9 213 21 4 214 I created a unique id for each row: dat$UniqueID - paste(dat$Date,dat$Time, sep = '_') then aggregate(dat$Var, list(dat$UniqueID), sum) however the final output is not in ideal order I look for (I simply this example provided above).I would like to have order like this: 1_1 1_2 2_1 Thanks for your help! [[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] combine two columns into one
Hi, May be I misunderstood your question: dat- read.table(text= Date Time Var 1 1 2 1 1 4 1 1 5 1 2 8 1 2 8 1 2 9 2 1 3 2 1 4 2 1 4 ,sep=,header=TRUE) dat$UniqueID - paste(dat$Date,dat$Time, sep = '_') aggregate(dat$Var,list(dat$UniqueID),sum) #isn't this the correct order # Group.1 x #1 1_1 11 #2 1_2 25 #3 2_1 11 library(plyr) ddply(dat,.(UniqueID),summarize,Var=sum(Var)) # UniqueID Var #1 1_1 11 #2 1_2 25 #3 2_1 11 A.K. - Original Message - From: Ye Lin ye...@lbl.gov To: R help r-help@r-project.org Cc: Sent: Wednesday, May 29, 2013 2:23 PM Subject: [R] combine two columns into one Hey all! I have a time series dataset like this: Date Time Var 1 1 2 1 1 4 1 1 5 1 2 8 1 2 8 1 2 9 2 1 3 2 1 4 2 1 4 I created a unique id for each row: dat$UniqueID - paste(dat$Date,dat$Time, sep = '_') then aggregate(dat$Var, list(dat$UniqueID), sum) however the final output is not in ideal order I look for (I simply this example provided above).I would like to have order like this: 1_1 1_2 2_1 Thanks for your help! [[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] combine two columns into one
The actual date col is in -mm-dd format, and when I apply this code to my actual data, it mess up the order On Wed, May 29, 2013 at 11:37 AM, arun smartpink...@yahoo.com wrote: Hi, May be I misunderstood your question: dat- read.table(text= DateTime Var 112 1 14 1 15 1 2 8 1 2 8 12 9 213 21 4 214 ,sep=,header=TRUE) dat$UniqueID - paste(dat$Date,dat$Time, sep = '_') aggregate(dat$Var,list(dat$UniqueID),sum) #isn't this the correct order # Group.1 x #1 1_1 11 #2 1_2 25 #3 2_1 11 library(plyr) ddply(dat,.(UniqueID),summarize,Var=sum(Var)) # UniqueID Var #1 1_1 11 #2 1_2 25 #3 2_1 11 A.K. - Original Message - From: Ye Lin ye...@lbl.gov To: R help r-help@r-project.org Cc: Sent: Wednesday, May 29, 2013 2:23 PM Subject: [R] combine two columns into one Hey all! I have a time series dataset like this: DateTime Var 112 1 14 1 1 5 1 2 8 1 2 8 1 2 9 213 21 4 214 I created a unique id for each row: dat$UniqueID - paste(dat$Date,dat$Time, sep = '_') then aggregate(dat$Var, list(dat$UniqueID), sum) however the final output is not in ideal order I look for (I simply this example provided above).I would like to have order like this: 1_1 1_2 2_1 Thanks for your help! [[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.
Re: [R] combine two columns into one
dat1- read.table(text= Date Time Var 11/1/2012 1 3 11/1/2012 1 1 11/1/2012 1 1 11/1/2012 2 3 11/1/2012 2 1 11/1/2012 2 1 11/2/2012 1 1 11/2/2012 1 3 11/2/2012 1 1 11/2/2012 2 3 ,sep=,header=TRUE,stringsAsFactors=FALSE) dat1$Date-as.Date(dat1$Date,format=%m/%d/%Y) If you need to combine Date and Time columns into one: library(plyr) res-mutate(ddply(dat1,.(Date,Time),summarize,Var=sum(Var)),DT=paste(Date,Time,sep=_))[,c(4,3)] res # DT Var #1 2012-11-01_1 5 #2 2012-11-01_2 5 #3 2012-11-02_1 5 #4 2012-11-02_2 3 A.K. From: Ye Lin ye...@lbl.gov To: arun smartpink...@yahoo.com Cc: R help r-help@r-project.org Sent: Wednesday, May 29, 2013 2:40 PM Subject: Re: [R] combine two columns into one The actual date col is in -mm-dd format, and when I apply this code to my actual data, it mess up the order On Wed, May 29, 2013 at 11:37 AM, arun smartpink...@yahoo.com wrote: Hi, May be I misunderstood your question: dat- read.table(text= Date Time Var 1 1 2 1 1 4 1 1 5 1 2 8 1 2 8 1 2 9 2 1 3 2 1 4 2 1 4 ,sep=,header=TRUE) dat$UniqueID - paste(dat$Date,dat$Time, sep = '_') aggregate(dat$Var,list(dat$UniqueID),sum) #isn't this the correct order # Group.1 x #1 1_1 11 #2 1_2 25 #3 2_1 11 library(plyr) ddply(dat,.(UniqueID),summarize,Var=sum(Var)) # UniqueID Var #1 1_1 11 #2 1_2 25 #3 2_1 11 A.K. - Original Message - From: Ye Lin ye...@lbl.gov To: R help r-help@r-project.org Cc: Sent: Wednesday, May 29, 2013 2:23 PM Subject: [R] combine two columns into one Hey all! I have a time series dataset like this: Date Time Var 1 1 2 1 1 4 1 1 5 1 2 8 1 2 8 1 2 9 2 1 3 2 1 4 2 1 4 I created a unique id for each row: dat$UniqueID - paste(dat$Date,dat$Time, sep = '_') then aggregate(dat$Var, list(dat$UniqueID), sum) however the final output is not in ideal order I look for (I simply this example provided above).I would like to have order like this: 1_1 1_2 2_1 Thanks for your help! [[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] Combine multiple tables into one
Hi, May be this helps: dat1- as.data.frame(table1) dat2- as.data.frame(table2) names(dat2)-c(V3,V4) library(plyr) res-join(dat1,dat2,type=full) res[is.na(res)]- 0 res # V1 V2 V3 V4 #1 1 1 0 0 #2 1 2 0 0 #3 0 0 0 1 #4 0 0 0 4 combinedtable-as.matrix(res) colnames(combinedtable)- NULL combinedtable # [,1] [,2] [,3] [,4] #[1,] 1 1 0 0 #[2,] 1 2 0 0 #[3,] 0 0 0 1 #[4,] 0 0 0 4 A.K. Hi, I am trying to combine multiple tables into one, where the elements that are created as a result of the merge to be filled with zeroes. In other words, to go from this: #Create tables to combine row1 - c(1,1) row2 - c(1,2) row3 - c(0,1) row4 - c(0,4) table1 - rbind(row1,row2) table2 - rbind(row3, row4) table1 [,1] [,2] row1 1 1 row2 1 2 table2 [,1] [,2] row3 0 1 row4 0 4 To this: #What the combined table should look like if things worked out newrow1 - c(1,1,0,0) newrow2 - c(1,2,0,0) newrow3 - c(0,0,0,1) newrow4 - c(0,0,0,4) combinedtable - rbind(newrow1, newrow2, newrow3, newrow4) combinedtable [,1] [,2] [,3] [,4] newrow1 1 1 0 0 newrow2 1 2 0 0 newrow3 0 0 0 1 newrow4 0 0 0 4 I tried merge() but it doesn't make any sense here, and I also tried to melt() the orginal data that table1 and table2 are created from to re-created a combined table from one step further back from this but couldn't get it to form the right table. If that would be an easier solution, here is the starting point that table1 and table2 are created from and also the create.table() function I wrote to create them: create.table - function(a,b){ obs - unique(c(a,b)) squ - matrix(rep(0,length(obs)^2),ncol=length(obs)) for(i in 1:length(obs)){ for(j in 1:length(obs)){ squ[i,j] - sum((a==obs[i])*(b==obs[j]), na.rm=TRUE) } } squ } #Mock data sampleid - c(1:5) Q1before - c(0,0,1,0,1) Q1after - c(0,1,0,0,1) Q2before - c(1,0,0,0,0) Q2after - c(0,0,0,0,0) #This is what my real data looks like #It may be easier to reformat this df to a format that could then use my create.table() function to get to the combined table endpoint? mydf - as.data.frame(cbind(sampleid,Q1before, Q1after, Q2before, Q2after)) create.table(mydf$Q1before, mydf$Q1after) create.table(mydf$Q2before, mydf$Q2after) I hope at least some of this makes sense. Thank you for your input, you guys are always the best! __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Combine multiple tables into one
add2blocks - function(m1, m2) { res - cbind(m1, matrix(0, dim(m2)[1], dim(m2)[2]) ) res - rbind(res, cbind( matrix(0, dim(m1)[1], dim(m1)[2]), m2) ) } new - add2block(table1, table2) new #--- [,1] [,2] [,3] [,4] row11100 row21200 row30001 row40004 On May 1, 2013, at 12:37 PM, arun wrote: Hi, May be this helps: dat1- as.data.frame(table1) dat2- as.data.frame(table2) names(dat2)-c(V3,V4) library(plyr) res-join(dat1,dat2,type=full) res[is.na(res)]- 0 res # V1 V2 V3 V4 #1 1 1 0 0 #2 1 2 0 0 #3 0 0 0 1 #4 0 0 0 4 combinedtable-as.matrix(res) colnames(combinedtable)- NULL combinedtable # [,1] [,2] [,3] [,4] #[1,]1100 #[2,]1200 #[3,]0001 #[4,]0004 A.K. Hi, I am trying to combine multiple tables into one, where the elements that are created as a result of the merge to be filled with zeroes. In other words, to go from this: #Create tables to combine row1 - c(1,1) row2 - c(1,2) row3 - c(0,1) row4 - c(0,4) table1 - rbind(row1,row2) table2 - rbind(row3, row4) table1 [,1] [,2] row111 row212 table2 [,1] [,2] row301 row404 To this: #What the combined table should look like if things worked out newrow1 - c(1,1,0,0) newrow2 - c(1,2,0,0) newrow3 - c(0,0,0,1) newrow4 - c(0,0,0,4) combinedtable - rbind(newrow1, newrow2, newrow3, newrow4) combinedtable [,1] [,2] [,3] [,4] newrow11100 newrow21200 newrow30001 newrow40004 I tried merge() but it doesn't make any sense here, and I also tried to melt() the orginal data that table1 and table2 are created from to re-created a combined table from one step further back from this but couldn't get it to form the right table. If that would be an easier solution, here is the starting point that table1 and table2 are created from and also the create.table() function I wrote to create them: create.table - function(a,b){ obs - unique(c(a,b)) squ - matrix(rep(0,length(obs)^2),ncol=length(obs)) for(i in 1:length(obs)){ for(j in 1:length(obs)){ squ[i,j] - sum((a==obs[i])*(b==obs[j]), na.rm=TRUE) } } squ } #Mock data sampleid - c(1:5) Q1before - c(0,0,1,0,1) Q1after - c(0,1,0,0,1) Q2before - c(1,0,0,0,0) Q2after - c(0,0,0,0,0) #This is what my real data looks like #It may be easier to reformat this df to a format that could then use my create.table() function to get to the combined table endpoint? mydf - as.data.frame(cbind(sampleid,Q1before, Q1after, Q2before, Q2after)) create.table(mydf$Q1before, mydf$Q1after) create.table(mydf$Q2before, mydf$Q2after) I hope at least some of this makes sense. Thank you for your input, you guys are always the best! __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. David Winsemius Alameda, CA, USA __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Combine multiple tables into one
Isn't this just a block diagonal matrix? library(pracma) blkdiag(table1, table2) [,1] [,2] [,3] [,4] [1,]1100 [2,]1200 [3,]0001 [4,]0004 Dennis On Wed, May 1, 2013 at 5:41 PM, David Winsemius dwinsem...@comcast.net wrote: add2blocks - function(m1, m2) { res - cbind(m1, matrix(0, dim(m2)[1], dim(m2)[2]) ) res - rbind(res, cbind( matrix(0, dim(m1)[1], dim(m1)[2]), m2) ) } new - add2block(table1, table2) new #--- [,1] [,2] [,3] [,4] row11100 row21200 row30001 row40004 On May 1, 2013, at 12:37 PM, arun wrote: Hi, May be this helps: dat1- as.data.frame(table1) dat2- as.data.frame(table2) names(dat2)-c(V3,V4) library(plyr) res-join(dat1,dat2,type=full) res[is.na(res)]- 0 res # V1 V2 V3 V4 #1 1 1 0 0 #2 1 2 0 0 #3 0 0 0 1 #4 0 0 0 4 combinedtable-as.matrix(res) colnames(combinedtable)- NULL combinedtable # [,1] [,2] [,3] [,4] #[1,]1100 #[2,]1200 #[3,]0001 #[4,]0004 A.K. Hi, I am trying to combine multiple tables into one, where the elements that are created as a result of the merge to be filled with zeroes. In other words, to go from this: #Create tables to combine row1 - c(1,1) row2 - c(1,2) row3 - c(0,1) row4 - c(0,4) table1 - rbind(row1,row2) table2 - rbind(row3, row4) table1 [,1] [,2] row111 row212 table2 [,1] [,2] row301 row404 To this: #What the combined table should look like if things worked out newrow1 - c(1,1,0,0) newrow2 - c(1,2,0,0) newrow3 - c(0,0,0,1) newrow4 - c(0,0,0,4) combinedtable - rbind(newrow1, newrow2, newrow3, newrow4) combinedtable [,1] [,2] [,3] [,4] newrow11100 newrow21200 newrow30001 newrow40004 I tried merge() but it doesn't make any sense here, and I also tried to melt() the orginal data that table1 and table2 are created from to re-created a combined table from one step further back from this but couldn't get it to form the right table. If that would be an easier solution, here is the starting point that table1 and table2 are created from and also the create.table() function I wrote to create them: create.table - function(a,b){ obs - unique(c(a,b)) squ - matrix(rep(0,length(obs)^2),ncol=length(obs)) for(i in 1:length(obs)){ for(j in 1:length(obs)){ squ[i,j] - sum((a==obs[i])*(b==obs[j]), na.rm=TRUE) } } squ } #Mock data sampleid - c(1:5) Q1before - c(0,0,1,0,1) Q1after - c(0,1,0,0,1) Q2before - c(1,0,0,0,0) Q2after - c(0,0,0,0,0) #This is what my real data looks like #It may be easier to reformat this df to a format that could then use my create.table() function to get to the combined table endpoint? mydf - as.data.frame(cbind(sampleid,Q1before, Q1after, Q2before, Q2after)) create.table(mydf$Q1before, mydf$Q1after) create.table(mydf$Q2before, mydf$Q2after) I hope at least some of this makes sense. Thank you for your input, you guys are always the best! __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. David Winsemius Alameda, CA, USA __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Combine multiple tables into one
#or library(magic) adiag(table1,table2) #rownames are preserved # [,1] [,2] [,3] [,4] #row1 1 1 0 0 #row2 1 2 0 0 #row3 0 0 0 1 #row4 0 0 0 4 A.K. - Original Message - From: Dennis Murphy djmu...@gmail.com To: David Winsemius dwinsem...@comcast.net Cc: arun smartpink...@yahoo.com; R help r-help@r-project.org Sent: Wednesday, May 1, 2013 10:47 PM Subject: Re: [R] Combine multiple tables into one Isn't this just a block diagonal matrix? library(pracma) blkdiag(table1, table2) [,1] [,2] [,3] [,4] [1,] 1 1 0 0 [2,] 1 2 0 0 [3,] 0 0 0 1 [4,] 0 0 0 4 Dennis On Wed, May 1, 2013 at 5:41 PM, David Winsemius dwinsem...@comcast.net wrote: add2blocks - function(m1, m2) { res - cbind(m1, matrix(0, dim(m2)[1], dim(m2)[2]) ) res - rbind(res, cbind( matrix(0, dim(m1)[1], dim(m1)[2]), m2) ) } new - add2block(table1, table2) new #--- [,1] [,2] [,3] [,4] row1 1 1 0 0 row2 1 2 0 0 row3 0 0 0 1 row4 0 0 0 4 On May 1, 2013, at 12:37 PM, arun wrote: Hi, May be this helps: dat1- as.data.frame(table1) dat2- as.data.frame(table2) names(dat2)-c(V3,V4) library(plyr) res-join(dat1,dat2,type=full) res[is.na(res)]- 0 res # V1 V2 V3 V4 #1 1 1 0 0 #2 1 2 0 0 #3 0 0 0 1 #4 0 0 0 4 combinedtable-as.matrix(res) colnames(combinedtable)- NULL combinedtable # [,1] [,2] [,3] [,4] #[1,] 1 1 0 0 #[2,] 1 2 0 0 #[3,] 0 0 0 1 #[4,] 0 0 0 4 A.K. Hi, I am trying to combine multiple tables into one, where the elements that are created as a result of the merge to be filled with zeroes. In other words, to go from this: #Create tables to combine row1 - c(1,1) row2 - c(1,2) row3 - c(0,1) row4 - c(0,4) table1 - rbind(row1,row2) table2 - rbind(row3, row4) table1 [,1] [,2] row1 1 1 row2 1 2 table2 [,1] [,2] row3 0 1 row4 0 4 To this: #What the combined table should look like if things worked out newrow1 - c(1,1,0,0) newrow2 - c(1,2,0,0) newrow3 - c(0,0,0,1) newrow4 - c(0,0,0,4) combinedtable - rbind(newrow1, newrow2, newrow3, newrow4) combinedtable [,1] [,2] [,3] [,4] newrow1 1 1 0 0 newrow2 1 2 0 0 newrow3 0 0 0 1 newrow4 0 0 0 4 I tried merge() but it doesn't make any sense here, and I also tried to melt() the orginal data that table1 and table2 are created from to re-created a combined table from one step further back from this but couldn't get it to form the right table. If that would be an easier solution, here is the starting point that table1 and table2 are created from and also the create.table() function I wrote to create them: create.table - function(a,b){ obs - unique(c(a,b)) squ - matrix(rep(0,length(obs)^2),ncol=length(obs)) for(i in 1:length(obs)){ for(j in 1:length(obs)){ squ[i,j] - sum((a==obs[i])*(b==obs[j]), na.rm=TRUE) } } squ } #Mock data sampleid - c(1:5) Q1before - c(0,0,1,0,1) Q1after - c(0,1,0,0,1) Q2before - c(1,0,0,0,0) Q2after - c(0,0,0,0,0) #This is what my real data looks like #It may be easier to reformat this df to a format that could then use my create.table() function to get to the combined table endpoint? mydf - as.data.frame(cbind(sampleid,Q1before, Q1after, Q2before, Q2after)) create.table(mydf$Q1before, mydf$Q1after) create.table(mydf$Q2before, mydf$Q2after) I hope at least some of this makes sense. Thank you for your input, you guys are always the best! __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. David Winsemius Alameda, CA, USA __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] combine similar variables in chart
I want to make a chart on variables which are taken in 2 different years on 2 the same locations, at different depths, where for every location the species and the amount of species are determined. The first chart I want to make is to see how much of every species is collected in total in a bar or piechart. Then I want to make a few charts with the species per location and per depth Here is a subset of my dataset Station Latitude(S) Longitude(E)Depth(m)Sa.Code Sp.Code N UPG02 -5.122355036119.339401 4 UPG02_4 Amphis_lesson 398 UPG02 -5.122355036119.339401 4 UPG02_4 Amphis_radiat 58 UPG02 -5.122355036119.339401 4 UPG02_4 Penero_planat 3 UPG02 -5.122355036119.339401 4.8 UPG02_4.8 Sphero_sp 5 UPG02 -5.122355036119.339401 4.8 UPG02_4.8 Amphis_lesson 204 UPG02 -5.122355036119.339401 4.8 UPG02_4.8 Amphis_radiat 18 UPG02 -5.122355036119.339401 9 UPG02_9 Sphero_sp 1 UPG02 -5.122355036119.339401 9 UPG02_9 Eponid_repand 2 UPG02 -5.122355036119.339401 9 UPG02_9 Planor_sp 7 UPG02 -5.122355036119.339401 15 UPG02_15Opercu_ammono 1 UPG02 -5.122355036119.339401 15 UPG02_15Hetero_depres 94 UPG02 -5.122355036119.339401 15 UPG02_15Amphis_lesson 101 UPG02 -5.122355036119.339401 18 UPG02_18Sphero_sp 3 UPG02 -5.122355036119.339401 18 UPG02_18Millio_sp 1 UPG02 -5.122355036119.339401 18 UPG02_18Calcar_mayori 56 UPG03 -5.136863021119.387289 3 UPG03_3 Millio_sp 2 UPG03 -5.136863021119.387289 3 UPG03_3 Sahuli_sp1 1 UPG03 -5.136863021119.387289 3 UPG03_3 Elphid_cratic 20 UPG03 -5.136863021119.387289 4 UPG03_4 Planor_sp 1 UPG03 -5.136863021119.387289 4 UPG03_4 Opercu_elegan 1 UPG03 -5.136863021119.387289 4 UPG03_4 Septot_sp 1 UPG03 -5.136863021119.387289 5 UPG03_5 Sphero_sp 1 UPG03 -5.136863021119.387289 5 UPG03_5 Millio_sp 1 UPG03 -5.136863021119.387289 5 UPG03_5 Ammoni_sp1 1 UPG03 -5.136863021119.387289 6 UPG03_6 Calcar_sp1 2 UPG03 -5.136863021119.387289 6 UPG03_6 Calcar_mayori 68 UPG03 -5.136863021119.387289 6 UPG03_6 Elphid_cratic 17 UPG03 -5.136863021119.387289 9 UPG03_9 Sphero_sp 2 UPG03 -5.136863021119.387289 9 UPG03_9 Calcar_mayori 323 UPG03 -5.136863021119.387289 9 UPG03_9 Calcar_spengl 2 UPG28 -5.122355036119.339401 5 UPG28_5 Penero_planat 8 UPG28 -5.122355036119.339401 5 UPG28_5 Amphis_latera 1 UPG28 -5.122355036119.339401 5 UPG28_5 Sorite_sp 1 UPG28 -5.122355036119.339401 8 UPG28_8 Bilocu_sp3 1 UPG28 -5.122355036119.339401 8 UPG28_8 Verteb_sp 1 UPG28 -5.122355036119.339401 8 UPG28_8 Penero_planat 7 UPG28 -5.122355036119.339401 11 UPG28_11Placop_c.f. 1 UPG28 -5.122355036119.339401 11 UPG28_11Sphero_sp 2 UPG28 -5.122355036119.339401 11 UPG28_11Siphon_siphon 1 UPG28 -5.122355036119.339401 14 UPG28_14Sphero_sp 1 UPG28 -5.122355036119.339401 14 UPG28_14Sorite_sp 1 UPG28 -5.122355036119.339401 14 UPG28_14Spirol_hadaii 1 UPG28 -5.122355036119.339401 17 UPG28_17Sphero_sp 3 UPG28 -5.122355036119.339401 17 UPG28_17Siphon_siphon 1 UPG28 -5.122355036119.339401 17 UPG28_17Sorite_sp 1 UPG32 -5.136863021119.387289 4 UPG32_4 Pyrgo_striol1 UPG32 -5.136863021119.387289 4 UPG32_4 Ammoma_alveol 1 UPG32 -5.136863021119.387289 4 UPG32_4 Quinqu_netstr 1 UPG32 -5.136863021119.387289 6 UPG32_6 Neorot_calcar 1 UPG32 -5.136863021119.387289 6 UPG32_6 Quinqu_c.f.ag 1 UPG32 -5.136863021119.387289 6 UPG32_6 Amphis_lesson 50 UPG32 -5.136863021119.387289 8 UPG32_8 Spirol_sp3 1 UPG32 -5.136863021119.387289 8 UPG32_8 Triloc_tricar 1 UPG32 -5.136863021119.387289 8 UPG32_8 Amphis_lesson 34 -- View this message in context: http://r.789695.n4.nabble.com/combine-similar-variables-in-chart-tp4649581.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
Re: [R] combine similar variables in chart
You've included three I wants in your message, but no question. Are you looking for functions? Try searching ... in R, on google, on http://rseek.org/, ... Have you tried some code, but can't get it to work? Send your code with error messages, and tell us precisely what you trying to get your code to do. Did you want us to do your homework? Good luck with that. Jean Marcella m.e.j.meerk...@students.uu.nl wrote on 11/15/2012 04:36:56 AM: I want to make a chart on variables which are taken in 2 different years on 2 the same locations, at different depths, where for every location the species and the amount of species are determined. The first chart I want to make is to see how much of every species is collected in total in a bar or piechart. Then I want to make a few charts with the species per location and per depth Here is a subset of my dataset Station Latitude(S) Longitude(E) Depth(m) Sa.Code Sp.Code N UPG02 -5.122355036 119.339401 4 UPG02_4 Amphis_lesson 398 UPG02 -5.122355036 119.339401 4 UPG02_4 Amphis_radiat 58 UPG02 -5.122355036 119.339401 4 UPG02_4 Penero_planat 3 UPG02 -5.122355036 119.339401 4.8 UPG02_4.8 Sphero_sp 5 UPG02 -5.122355036 119.339401 4.8 UPG02_4.8 Amphis_lesson 204 UPG02 -5.122355036 119.339401 4.8 UPG02_4.8 Amphis_radiat 18 UPG02 -5.122355036 119.339401 9 UPG02_9 Sphero_sp 1 UPG02 -5.122355036 119.339401 9 UPG02_9 Eponid_repand 2 UPG02 -5.122355036 119.339401 9 UPG02_9 Planor_sp 7 UPG02 -5.122355036 119.339401 15 UPG02_15 Opercu_ammono 1 UPG02 -5.122355036 119.339401 15 UPG02_15 Hetero_depres 94 UPG02 -5.122355036 119.339401 15 UPG02_15 Amphis_lesson 101 UPG02 -5.122355036 119.339401 18 UPG02_18 Sphero_sp 3 UPG02 -5.122355036 119.339401 18 UPG02_18 Millio_sp 1 UPG02 -5.122355036 119.339401 18 UPG02_18 Calcar_mayori 56 UPG03 -5.136863021 119.387289 3 UPG03_3 Millio_sp 2 UPG03 -5.136863021 119.387289 3 UPG03_3 Sahuli_sp1 1 UPG03 -5.136863021 119.387289 3 UPG03_3 Elphid_cratic 20 UPG03 -5.136863021 119.387289 4 UPG03_4 Planor_sp 1 UPG03 -5.136863021 119.387289 4 UPG03_4 Opercu_elegan 1 UPG03 -5.136863021 119.387289 4 UPG03_4 Septot_sp 1 UPG03 -5.136863021 119.387289 5 UPG03_5 Sphero_sp 1 UPG03 -5.136863021 119.387289 5 UPG03_5 Millio_sp 1 UPG03 -5.136863021 119.387289 5 UPG03_5 Ammoni_sp1 1 UPG03 -5.136863021 119.387289 6 UPG03_6 Calcar_sp1 2 UPG03 -5.136863021 119.387289 6 UPG03_6 Calcar_mayori 68 UPG03 -5.136863021 119.387289 6 UPG03_6 Elphid_cratic 17 UPG03 -5.136863021 119.387289 9 UPG03_9 Sphero_sp 2 UPG03 -5.136863021 119.387289 9 UPG03_9 Calcar_mayori 323 UPG03 -5.136863021 119.387289 9 UPG03_9 Calcar_spengl 2 UPG28 -5.122355036 119.339401 5 UPG28_5 Penero_planat 8 UPG28 -5.122355036 119.339401 5 UPG28_5 Amphis_latera 1 UPG28 -5.122355036 119.339401 5 UPG28_5 Sorite_sp 1 UPG28 -5.122355036 119.339401 8 UPG28_8 Bilocu_sp3 1 UPG28 -5.122355036 119.339401 8 UPG28_8 Verteb_sp 1 UPG28 -5.122355036 119.339401 8 UPG28_8 Penero_planat 7 UPG28 -5.122355036 119.339401 11 UPG28_11 Placop_c.f. 1 UPG28 -5.122355036 119.339401 11 UPG28_11 Sphero_sp 2 UPG28 -5.122355036 119.339401 11 UPG28_11 Siphon_siphon 1 UPG28 -5.122355036 119.339401 14 UPG28_14 Sphero_sp 1 UPG28 -5.122355036 119.339401 14 UPG28_14 Sorite_sp 1 UPG28 -5.122355036 119.339401 14 UPG28_14 Spirol_hadaii 1 UPG28 -5.122355036 119.339401 17 UPG28_17 Sphero_sp 3 UPG28 -5.122355036 119.339401 17 UPG28_17 Siphon_siphon 1 UPG28 -5.122355036 119.339401 17 UPG28_17 Sorite_sp 1 UPG32 -5.136863021 119.387289 4 UPG32_4 Pyrgo_striol 1 UPG32 -5.136863021 119.387289 4 UPG32_4 Ammoma_alveol 1 UPG32 -5.136863021 119.387289 4 UPG32_4 Quinqu_netstr 1 UPG32 -5.136863021 119.387289 6 UPG32_6 Neorot_calcar 1 UPG32 -5.136863021 119.387289 6 UPG32_6 Quinqu_c.f.ag 1 UPG32 -5.136863021 119.387289 6 UPG32_6 Amphis_lesson 50 UPG32 -5.136863021 119.387289 8 UPG32_8 Spirol_sp3 1 UPG32 -5.136863021 119.387289 8 UPG32_8 Triloc_tricar 1 UPG32 -5.136863021 119.387289 8 UPG32_8 Amphis_lesson 34 [[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] combine unadjusted and adjusted forest plots
Dear Romita, It is not quite clear to me what exactly you want the graph to look like. What do you mean by plots of the unadjusted and adjusted models? In general though, it sounds to me as if the graph you have in mind is rather complex. It may be possible to accomplish this with the forest() and addpoly() functions in the metafor package, but I can't say for sure. Essentially, what I would have to see first is a hand-sketch of the graph you would like to draw. At a certain point though, it may be easier to just create the graph with the low-level plotting functions in R (points, lines, segments, polygon, etc.). Best, Wolfgang -- Wolfgang Viechtbauer, Ph.D., Statistician Department of Psychiatry and Psychology School for Mental Health and Neuroscience Faculty of Health, Medicine, and Life Sciences Maastricht University, P.O. Box 616 (VIJV1) 6200 MD Maastricht, The Netherlands +31 (43) 388-4170 | http://www.wvbauer.com -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Romita Mukerjee, M.D. Sent: Wednesday, October 10, 2012 21:43 To: r-help@r-project.org Subject: [R] combine unadjusted and adjusted forest plots Hello, I am learning to use the metafor package to conduct meta-regression analyses for a systematic review on multidisciplinary care interventions in chronic kidney disease. For the forest plots, I can't figure out how to plot unadjusted and adjusted models on the same plot. From top to bottom, I would like to be able have the unadjusted plot, the multivariate adjusted plot, then each univariate adjusted plot. Below each plot I would like to also include a polygon with the respective summary estimate and confidence interval. I would also like to have only one x-axis at the bottom of the whole plot. The moderators for the meta-regression include both continuous and categorical variables. Any thoughts? Thanks, Romi Mukerjee Romita Mukerjee, M.D. Nephrology Fellow, PGY-6 Duke University Medical Center [[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] combine unadjusted and adjusted forest plots
Hello, I am learning to use the metafor package to conduct meta-regression analyses for a systematic review on multidisciplinary care interventions in chronic kidney disease. For the forest plots, I can't figure out how to plot unadjusted and adjusted models on the same plot. From top to bottom, I would like to be able have the unadjusted plot, the multivariate adjusted plot, then each univariate adjusted plot. Below each plot I would like to also include a polygon with the respective summary estimate and confidence interval. I would also like to have only one x-axis at the bottom of the whole plot. The moderators for the meta-regression include both continuous and categorical variables. Any thoughts? Thanks, Romi Mukerjee Romita Mukerjee, M.D. Nephrology Fellow, PGY-6 Duke University Medical Center [[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] Combine two variables
Hi: I have two variables in a data frame that are the results of a wording experiment in a survey. I'd like to create a third variable that combines the two variables. Recode doesn't seem to work, because it just recodes the first variable into the third, then recodes the second variable into the third, overwriting the first recode. I can do this with a rather elaborate indexing process, subsetting the first column and then copying the data into the second etc. But I'm looking for a cleaner way to do this. The data frame looks like this. df-data.frame(var1=sample(c('a','b','c',NA),replace=TRUE, size=100), var2=sample(c('a','b','c',NA),replace=TRUE,size=100)) df-subset(df, !is.na(var1) |!is.na(var2)) As you can see, if one variable has an NA, then the other variable has a valid value, so how do I just combine the two variables into one? Thank you for your assistance. Simon Kiss __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Combine two variables
Hello, Inline. Em 11-09-2012 15:57, Simon Kiss escreveu: Hi: I have two variables in a data frame that are the results of a wording experiment in a survey. I'd like to create a third variable that combines the two variables. Recode doesn't seem to work, because it just recodes the first variable into the third, then recodes the second variable into the third, overwriting the first recode. I can do this with a rather elaborate indexing process, subsetting the first column and then copying the data into the second etc. But I'm looking for a cleaner way to do this. The data frame looks like this. df-data.frame(var1=sample(c('a','b','c',NA),replace=TRUE, size=100), var2=sample(c('a','b','c',NA),replace=TRUE,size=100)) df-subset(df, !is.na(var1) |!is.na(var2)) As you can see, if one variable has an NA, then the other variable has a valid value, No, not necessarily. You are using sample() and there's no reason to believe the sampled values for var1 and var2 are going to be different. My first try gave me several rows with both columns NA. Then I've used set.seed() and it became reproducible. set.seed(1) df1 - data.frame(var1=sample(c('a','b','c',NA), replace=TRUE, size=100), var2=sample(c('a','b','c',NA), replace=TRUE, size=100)) sum(is.na(df1$var1) is.na(df1$var2)) # 8 So I suppose this is not the case with your real dataset. Try the following. df1$var3 - df1$var1 df1$var3[is.na(df1$var1)] - df1$var2[is.na(df1$var1)] Hope this helps, Rui Barradas so how do I just combine the two variables into one? Thank you for your assistance. Simon Kiss __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Combine two variables
Hi I am not sure I understand correctly. In the sample dataframe you posted, the values in columns are different so based on what you did write I aasume that apply(df,1, paste, collapse=) gives you third variable combined from those 2 variables. If you want to select non NA value from any variable, which one will you select when there is no NA in some row? Regards Petr -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- project.org] On Behalf Of Simon Kiss Sent: Tuesday, September 11, 2012 4:57 PM To: r-help@r-project.org Subject: [R] Combine two variables Hi: I have two variables in a data frame that are the results of a wording experiment in a survey. I'd like to create a third variable that combines the two variables. Recode doesn't seem to work, because it just recodes the first variable into the third, then recodes the second variable into the third, overwriting the first recode. I can do this with a rather elaborate indexing process, subsetting the first column and then copying the data into the second etc. But I'm looking for a cleaner way to do this. The data frame looks like this. df-data.frame(var1=sample(c('a','b','c',NA),replace=TRUE, size=100), var2=sample(c('a','b','c',NA),replace=TRUE,size=100)) df-subset(df, !is.na(var1) |!is.na(var2)) As you can see, if one variable has an NA, then the other variable has a valid value, so how do I just combine the two variables into one? Thank you for your assistance. Simon Kiss __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Combine two variables
Hi, I am not sure how you describe combine. Try this: df1-subset(df, !is.na(var1) !is.na(var2)) df1$new-paste0(df1$var1,df1$var2) head(df1) # var1 var2 new #1 b a ba #2 c b cb #3 b b bb #5 a a aa #6 b b bb #7 a b ab A.K. - Original Message - From: Simon Kiss sjk...@gmail.com To: r-help@r-project.org Cc: Sent: Tuesday, September 11, 2012 10:57 AM Subject: [R] Combine two variables Hi: I have two variables in a data frame that are the results of a wording experiment in a survey. I'd like to create a third variable that combines the two variables. Recode doesn't seem to work, because it just recodes the first variable into the third, then recodes the second variable into the third, overwriting the first recode. I can do this with a rather elaborate indexing process, subsetting the first column and then copying the data into the second etc. But I'm looking for a cleaner way to do this. The data frame looks like this. df-data.frame(var1=sample(c('a','b','c',NA),replace=TRUE, size=100), var2=sample(c('a','b','c',NA),replace=TRUE,size=100)) df-subset(df, !is.na(var1) |!is.na(var2)) As you can see, if one variable has an NA, then the other variable has a valid value, so how do I just combine the two variables into one? Thank you for your assistance. Simon Kiss __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Combine subsets by factor level
We really should have some sample data (see dput() for a handy way to post some). However have a look at ?reshape, the plyr and reshape packages or perhaps the data.table package. John Kane Kingston ON Canada -Original Message- From: libgray3...@gmail.com Sent: Tue, 5 Jun 2012 21:07:50 -0700 (PDT) To: r-help@r-project.org Subject: [R] Combine subsets by factor level I'm attempting to change a data set by compressing rows into columns. Currently there are several rows that all have information about one patient, but at different cycles. I'm trying to make each patient only have one row in the data set. Does anyone know a good way to combine data sets by factor level? I've separated the groups into different subsets by cycle, but not every patient has data for every cycle (i.e. there are 1200 who have cycle 0, but only 200 of those have a cycle 1, and a different number have cycles higher than that, etc). I then made the patient number the identifying label. If there is a way to column-combine these subsets by the factor level of these patient names, and leave any patients that are missing a cycle as NA? If anyone has insight on how to do this, or a better way to complete what I'm trying to do, I'd appreciate it! -- View this message in context: http://r.789695.n4.nabble.com/Combine-subsets-by-factor-level-tp4632472.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. FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop! __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] Combine subsets by factor level
Hi Iglucia, I am not sure how your dataset looks like. Does it look similar to this: dat4-data.frame(patient=rep(c(1:10), rep(3,10)),var=rep(c(cycle0,cycle1,cycle2),rep(1,3)),value=c(rnorm(15,1,0.5),NA,rnorm(5,1,0.5),NA,rnorm(8,1,0.5))) dat4 patient var value 1 1 cycle0 1.8826827 2 1 cycle1 1.0316985 3 1 cycle2 1.0084754 4 2 cycle0 1.1822553 5 2 cycle1 1.5494087 6 2 cycle2 0.9173749 7 3 cycle0 0.3935503 8 3 cycle1 0.7012282 9 3 cycle2 0.5213031 10 4 cycle0 0.8330390 11 4 cycle1 0.6430550 12 4 cycle2 0.7751283 13 5 cycle0 1.4092714 14 5 cycle1 0.8120330 15 5 cycle2 0.6255491 16 6 cycle0 NA 17 6 cycle1 0.1068520 18 6 cycle2 0.7556006 19 7 cycle0 1.4322698 20 7 cycle1 1.6109262 21 7 cycle2 0.9650534 22 8 cycle0 NA 23 8 cycle1 0.3861208 24 8 cycle2 1.1349206 25 9 cycle0 1.5659958 26 9 cycle1 1.8725942 27 9 cycle2 1.5676570 28 10 cycle0 1.0895054 29 10 cycle1 1.1941775 30 10 cycle2 1.3932515 A.K. - Original Message - From: lglucia libgray3...@gmail.com To: r-help@r-project.org Cc: Sent: Wednesday, June 6, 2012 12:07 AM Subject: [R] Combine subsets by factor level I'm attempting to change a data set by compressing rows into columns. Currently there are several rows that all have information about one patient, but at different cycles. I'm trying to make each patient only have one row in the data set. Does anyone know a good way to combine data sets by factor level? I've separated the groups into different subsets by cycle, but not every patient has data for every cycle (i.e. there are 1200 who have cycle 0, but only 200 of those have a cycle 1, and a different number have cycles higher than that, etc). I then made the patient number the identifying label. If there is a way to column-combine these subsets by the factor level of these patient names, and leave any patients that are missing a cycle as NA? If anyone has insight on how to do this, or a better way to complete what I'm trying to do, I'd appreciate it! -- View this message in context: http://r.789695.n4.nabble.com/Combine-subsets-by-factor-level-tp4632472.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] Combine subsets by factor level
Yes, except that patients have different cycle numbers. Such as, one might have cycle 1,2,3, and another has 1,4,12. On Jun 6, 2012 12:54 PM, arun smartpink...@yahoo.com wrote: Hi Iglucia, I am not sure how your dataset looks like. Does it look similar to this: dat4-data.frame(patient=rep(c(1:10), rep(3,10)),var=rep(c(cycle0,cycle1,cycle2),rep(1,3)),value=c(rnorm(15,1,0.5),NA,rnorm(5,1,0.5),NA,rnorm(8,1,0.5))) dat4 patientvar value 11 cycle0 1.8826827 21 cycle1 1.0316985 31 cycle2 1.0084754 42 cycle0 1.1822553 52 cycle1 1.5494087 62 cycle2 0.9173749 73 cycle0 0.3935503 83 cycle1 0.7012282 93 cycle2 0.5213031 10 4 cycle0 0.8330390 11 4 cycle1 0.6430550 12 4 cycle2 0.7751283 13 5 cycle0 1.4092714 14 5 cycle1 0.8120330 15 5 cycle2 0.6255491 16 6 cycle0NA 17 6 cycle1 0.1068520 18 6 cycle2 0.7556006 19 7 cycle0 1.4322698 20 7 cycle1 1.6109262 21 7 cycle2 0.9650534 22 8 cycle0NA 23 8 cycle1 0.3861208 24 8 cycle2 1.1349206 25 9 cycle0 1.5659958 26 9 cycle1 1.8725942 27 9 cycle2 1.5676570 28 10 cycle0 1.0895054 29 10 cycle1 1.1941775 30 10 cycle2 1.3932515 A.K. - Original Message - From: lglucia libgray3...@gmail.com To: r-help@r-project.org Cc: Sent: Wednesday, June 6, 2012 12:07 AM Subject: [R] Combine subsets by factor level I'm attempting to change a data set by compressing rows into columns. Currently there are several rows that all have information about one patient, but at different cycles. I'm trying to make each patient only have one row in the data set. Does anyone know a good way to combine data sets by factor level? I've separated the groups into different subsets by cycle, but not every patient has data for every cycle (i.e. there are 1200 who have cycle 0, but only 200 of those have a cycle 1, and a different number have cycles higher than that, etc). I then made the patient number the identifying label. If there is a way to column-combine these subsets by the factor level of these patient names, and leave any patients that are missing a cycle as NA? If anyone has insight on how to do this, or a better way to complete what I'm trying to do, I'd appreciate it! -- View this message in context: http://r.789695.n4.nabble.com/Combine-subsets-by-factor-level-tp4632472.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.
Re: [R] Combine subsets by factor level
Hi, Try this: library(reshape) dat4-data.frame(patient=rep(c(1:10), rep(3,10)),var=rep(c(cycle0,cycle1,cycle2),rep(1,3)),value=c(rnorm(15,1,0.5),NA,rnorm(5,1,0.5),NA,rnorm(8,1,0.5))) dat5-cast(dat4,patient~var,value=value) dat5 patient cycle0 cycle1 cycle2 1 1 1.8826827 1.0316985 1.0084754 2 2 1.1822553 1.5494087 0.9173749 3 3 0.3935503 0.7012282 0.5213031 4 4 0.8330390 0.6430550 0.7751283 5 5 1.4092714 0.8120330 0.6255491 6 6 NA 0.1068520 0.7556006 7 7 1.4322698 1.6109262 0.9650534 8 8 NA 0.3861208 1.1349206 9 9 1.5659958 1.8725942 1.5676570 10 10 1.0895054 1.1941775 1.3932515 For the missing values, I assume that cycle will be in the dataset on the longformat and its value as NA. A.K. From: Lib Gray libgray3...@gmail.com To: arun smartpink...@yahoo.com Cc: R help r-help@r-project.org Sent: Wednesday, June 6, 2012 2:28 PM Subject: Re: [R] Combine subsets by factor level Yes, except that patients have different cycle numbers. Such as, one might have cycle 1,2,3, and another has 1,4,12. On Jun 6, 2012 12:54 PM, arun smartpink...@yahoo.com wrote: Hi Iglucia, I am not sure how your dataset looks like. Does it look similar to this: dat4-data.frame(patient=rep(c(1:10), rep(3,10)),var=rep(c(cycle0,cycle1,cycle2),rep(1,3)),value=c(rnorm(15,1,0.5),NA,rnorm(5,1,0.5),NA,rnorm(8,1,0.5))) dat4 patient var value 1 1 cycle0 1.8826827 2 1 cycle1 1.0316985 3 1 cycle2 1.0084754 4 2 cycle0 1.1822553 5 2 cycle1 1.5494087 6 2 cycle2 0.9173749 7 3 cycle0 0.3935503 8 3 cycle1 0.7012282 9 3 cycle2 0.5213031 10 4 cycle0 0.8330390 11 4 cycle1 0.6430550 12 4 cycle2 0.7751283 13 5 cycle0 1.4092714 14 5 cycle1 0.8120330 15 5 cycle2 0.6255491 16 6 cycle0 NA 17 6 cycle1 0.1068520 18 6 cycle2 0.7556006 19 7 cycle0 1.4322698 20 7 cycle1 1.6109262 21 7 cycle2 0.9650534 22 8 cycle0 NA 23 8 cycle1 0.3861208 24 8 cycle2 1.1349206 25 9 cycle0 1.5659958 26 9 cycle1 1.8725942 27 9 cycle2 1.5676570 28 10 cycle0 1.0895054 29 10 cycle1 1.1941775 30 10 cycle2 1.3932515 A.K. - Original Message - From: lglucia libgray3...@gmail.com To: r-help@r-project.org Cc: Sent: Wednesday, June 6, 2012 12:07 AM Subject: [R] Combine subsets by factor level I'm attempting to change a data set by compressing rows into columns. Currently there are several rows that all have information about one patient, but at different cycles. I'm trying to make each patient only have one row in the data set. Does anyone know a good way to combine data sets by factor level? I've separated the groups into different subsets by cycle, but not every patient has data for every cycle (i.e. there are 1200 who have cycle 0, but only 200 of those have a cycle 1, and a different number have cycles higher than that, etc). I then made the patient number the identifying label. If there is a way to column-combine these subsets by the factor level of these patient names, and leave any patients that are missing a cycle as NA? If anyone has insight on how to do this, or a better way to complete what I'm trying to do, I'd appreciate it! -- View this message in context: http://r.789695.n4.nabble.com/Combine-subsets-by-factor-level-tp4632472.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] Combine subsets by factor level
They do not; each patient only has rows for the cycles it has. Some have only one, some have more than ten. On Jun 6, 2012 2:24 PM, arun smartpink...@yahoo.com wrote: Hi, Try this: library(reshape) dat4-data.frame(patient=rep(c(1:10), rep(3,10)),var=rep(c(cycle0,cycle1,cycle2),rep(1,3)),value=c(rnorm(15,1,0.5),NA,rnorm(5,1,0.5),NA,rnorm(8,1,0.5))) dat5-cast(dat4,patient~var,value=value) dat5 patientcycle0cycle1cycle2 11 1.8826827 1.0316985 1.0084754 22 1.1822553 1.5494087 0.9173749 33 0.3935503 0.7012282 0.5213031 44 0.8330390 0.6430550 0.7751283 55 1.4092714 0.8120330 0.6255491 66NA 0.1068520 0.7556006 77 1.4322698 1.6109262 0.9650534 88NA 0.3861208 1.1349206 99 1.5659958 1.8725942 1.5676570 10 10 1.0895054 1.1941775 1.3932515 For the missing values, I assume that cycle will be in the dataset on the longformat and its value as NA. A.K. From: Lib Gray libgray3...@gmail.com To: arun smartpink...@yahoo.com Cc: R help r-help@r-project.org Sent: Wednesday, June 6, 2012 2:28 PM Subject: Re: [R] Combine subsets by factor level Yes, except that patients have different cycle numbers. Such as, one might have cycle 1,2,3, and another has 1,4,12. On Jun 6, 2012 12:54 PM, arun smartpink...@yahoo.com wrote: Hi Iglucia, I am not sure how your dataset looks like. Does it look similar to this: dat4-data.frame(patient=rep(c(1:10), rep(3,10)),var=rep(c(cycle0,cycle1,cycle2),rep(1,3)),value=c(rnorm(15,1,0.5),NA,rnorm(5,1,0.5),NA,rnorm(8,1,0.5))) dat4 patientvar value 11 cycle0 1.8826827 21 cycle1 1.0316985 31 cycle2 1.0084754 42 cycle0 1.1822553 52 cycle1 1.5494087 62 cycle2 0.9173749 73 cycle0 0.3935503 83 cycle1 0.7012282 93 cycle2 0.5213031 10 4 cycle0 0.8330390 11 4 cycle1 0.6430550 12 4 cycle2 0.7751283 13 5 cycle0 1.4092714 14 5 cycle1 0.8120330 15 5 cycle2 0.6255491 16 6 cycle0NA 17 6 cycle1 0.1068520 18 6 cycle2 0.7556006 19 7 cycle0 1.4322698 20 7 cycle1 1.6109262 21 7 cycle2 0.9650534 22 8 cycle0NA 23 8 cycle1 0.3861208 24 8 cycle2 1.1349206 25 9 cycle0 1.5659958 26 9 cycle1 1.8725942 27 9 cycle2 1.5676570 28 10 cycle0 1.0895054 29 10 cycle1 1.1941775 30 10 cycle2 1.3932515 A.K. - Original Message - From: lglucia libgray3...@gmail.com To: r-help@r-project.org Cc: Sent: Wednesday, June 6, 2012 12:07 AM Subject: [R] Combine subsets by factor level I'm attempting to change a data set by compressing rows into columns. Currently there are several rows that all have information about one patient, but at different cycles. I'm trying to make each patient only have one row in the data set. Does anyone know a good way to combine data sets by factor level? I've separated the groups into different subsets by cycle, but not every patient has data for every cycle (i.e. there are 1200 who have cycle 0, but only 200 of those have a cycle 1, and a different number have cycles higher than that, etc). I then made the patient number the identifying label. If there is a way to column-combine these subsets by the factor level of these patient names, and leave any patients that are missing a cycle as NA? If anyone has insight on how to do this, or a better way to complete what I'm trying to do, I'd appreciate it! -- View this message in context: http://r.789695.n4.nabble.com/Combine-subsets-by-factor-level-tp4632472.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.
Re: [R] Combine subsets by factor level
Hi, So it might be in this format. Same code works. dat6-data.frame(patient=c(c(1:3)[rep(c(1,1,1,2,2,3,3,3,3))],rep(c(4:10),rep(3,7))), var=c(cycle0,cycle1,cycle2,cycle5,cycle12)[rep(c(1,2,3,1,5,1,2,3,4,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3))],value=c(rnorm(30,1,0.5))) dat6 patient var value 1 1 cycle0 1.56422778 2 1 cycle1 0.58855364 3 1 cycle2 0.80263892 4 2 cycle0 1.12141258 5 2 cycle12 1.59177846 6 3 cycle0 0.09706219 7 3 cycle1 0.62707806 8 3 cycle2 1.67569203 9 3 cycle5 1.46231543 10 4 cycle0 0.22468741 11 4 cycle1 1.78807770 12 4 cycle2 2.10534150 13 5 cycle0 0.71970367 14 5 cycle1 0.80972468 15 5 cycle2 0.37859704 16 6 cycle0 1.72754736 17 6 cycle1 0.24323640 18 6 cycle2 0.67442840 19 7 cycle0 0.73625734 20 7 cycle1 0.74946198 21 7 cycle2 0.05603698 22 8 cycle0 0.87371378 23 8 cycle1 1.21543691 24 8 cycle2 0.87648443 25 9 cycle0 1.28035157 26 9 cycle1 0.99059664 27 9 cycle2 1.01691054 28 10 cycle0 1.02381366 29 10 cycle1 0.64904309 30 10 cycle2 2.28296855 dat7-cast(dat6,patient~var,value=value) dat7 patient cycle0 cycle1 cycle12 cycle2 cycle5 1 1 1.56422778 0.5885536 NA 0.80263892 NA 2 2 1.12141258 NA 1.591778 NA NA 3 3 0.09706219 0.6270781 NA 1.67569203 1.462315 4 4 0.22468741 1.7880777 NA 2.10534150 NA 5 5 0.71970367 0.8097247 NA 0.37859704 NA 6 6 1.72754736 0.2432364 NA 0.67442840 NA 7 7 0.73625734 0.7494620 NA 0.05603698 NA 8 8 0.87371378 1.2154369 NA 0.87648443 NA 9 9 1.28035157 0.9905966 NA 1.01691054 NA 10 10 1.02381366 0.6490431 NA 2.28296855 NA A.K. From: Lib Gray libgray3...@gmail.com To: arun smartpink...@yahoo.com Cc: R help r-help@r-project.org Sent: Wednesday, June 6, 2012 4:52 PM Subject: Re: [R] Combine subsets by factor level They do not; each patient only has rows for the cycles it has. Some have only one, some have more than ten. On Jun 6, 2012 2:24 PM, arun smartpink...@yahoo.com wrote: Hi, Try this: library(reshape) dat4-data.frame(patient=rep(c(1:10), rep(3,10)),var=rep(c(cycle0,cycle1,cycle2),rep(1,3)),value=c(rnorm(15,1,0.5),NA,rnorm(5,1,0.5),NA,rnorm(8,1,0.5))) dat5-cast(dat4,patient~var,value=value) dat5 patient cycle0 cycle1 cycle2 1 1 1.8826827 1.0316985 1.0084754 2 2 1.1822553 1.5494087 0.9173749 3 3 0.3935503 0.7012282 0.5213031 4 4 0.8330390 0.6430550 0.7751283 5 5 1.4092714 0.8120330 0.6255491 6 6 NA 0.1068520 0.7556006 7 7 1.4322698 1.6109262 0.9650534 8 8 NA 0.3861208 1.1349206 9 9 1.5659958 1.8725942 1.5676570 10 10 1.0895054 1.1941775 1.3932515 For the missing values, I assume that cycle will be in the dataset on the longformat and its value as NA. A.K. From: Lib Gray libgray3...@gmail.com To: arun smartpink...@yahoo.com Cc: R help r-help@r-project.org Sent: Wednesday, June 6, 2012 2:28 PM Subject: Re: [R] Combine subsets by factor level Yes, except that patients have different cycle numbers. Such as, one might have cycle 1,2,3, and another has 1,4,12. On Jun 6, 2012 12:54 PM, arun smartpink...@yahoo.com wrote: Hi Iglucia, I am not sure how your dataset looks like. Does it look similar to this: dat4-data.frame(patient=rep(c(1:10), rep(3,10)),var=rep(c(cycle0,cycle1,cycle2),rep(1,3)),value=c(rnorm(15,1,0.5),NA,rnorm(5,1,0.5),NA,rnorm(8,1,0.5))) dat4 patient var value 1 1 cycle0 1.8826827 2 1 cycle1 1.0316985 3 1 cycle2 1.0084754 4 2 cycle0 1.1822553 5 2 cycle1 1.5494087 6 2 cycle2 0.9173749 7 3 cycle0 0.3935503 8 3 cycle1 0.7012282 9 3 cycle2 0.5213031 10 4 cycle0 0.8330390 11 4 cycle1 0.6430550 12 4 cycle2 0.7751283 13 5 cycle0 1.4092714 14 5 cycle1 0.8120330 15 5 cycle2 0.6255491 16 6 cycle0 NA 17 6 cycle1 0.1068520 18 6 cycle2 0.7556006 19 7 cycle0 1.4322698 20 7 cycle1 1.6109262 21 7 cycle2 0.9650534 22 8 cycle0 NA 23 8 cycle1 0.3861208 24 8 cycle2 1.1349206 25 9 cycle0 1.5659958 26 9 cycle1 1.8725942 27 9 cycle2 1.5676570 28 10 cycle0 1.0895054 29 10 cycle1 1.1941775 30 10 cycle2 1.3932515 A.K. - Original Message - From: lglucia libgray3...@gmail.com To: r-help@r-project.org Cc: Sent: Wednesday, June 6, 2012 12:07 AM Subject: [R] Combine subsets by factor level I'm attempting to change a data set by compressing rows into columns. Currently there are several rows that all have information about one
[R] Combine subsets by factor level
I'm attempting to change a data set by compressing rows into columns. Currently there are several rows that all have information about one patient, but at different cycles. I'm trying to make each patient only have one row in the data set. Does anyone know a good way to combine data sets by factor level? I've separated the groups into different subsets by cycle, but not every patient has data for every cycle (i.e. there are 1200 who have cycle 0, but only 200 of those have a cycle 1, and a different number have cycles higher than that, etc). I then made the patient number the identifying label. If there is a way to column-combine these subsets by the factor level of these patient names, and leave any patients that are missing a cycle as NA? If anyone has insight on how to do this, or a better way to complete what I'm trying to do, I'd appreciate it! -- View this message in context: http://r.789695.n4.nabble.com/Combine-subsets-by-factor-level-tp4632472.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] Combine two tables by row with different columns
Hi R user, I could not combine two tables. Would any one help me on how I can combine with following example tables? T1 X Y Z XX A 1 5 9 13 B 2 6 10 14 C 3 7 11 15 D 4 8 12 16 T2 X Y XX a 1 4 7 b 2 5 8 c 3 6 9 I want to get the following table cT X Y XX A 1 5 13 B 2 6 14 C 3 7 15 D 4 8 16 A1 1 4 7 B1 2 5 8 C1 3 6 9 Thanks for your help. 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.
Re: [R] Combine two tables by row with different columns
On May 9, 2012, at 9:21 PM, Kristi Glover wrote: Hi R user, I could not combine two tables. Would any one help me on how I can combine with following example tables? You do realize that tables in R are not dataframes. They are a type of matrix. T1 X Y Z XX A 1 5 9 13 B 2 6 10 14 C 3 7 11 15 D 4 8 12 16 T2 X Y XX a 1 4 7 b 2 5 8 c 3 6 9 I want to get the following table cT X Y XX A 1 5 13 B 2 6 14 C 3 7 15 D 4 8 16 A1 1 4 7 B1 2 5 8 C1 3 6 9 Assuming you will accept the rownames as they stand, then try this: cT - cbind(T1,T2) # works for table, matrices, and dataframes [[alternative HTML version deleted]] Plain text, please. -- 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] Combine two tables by row with different columns
Thanks David, I tried 'cbind as you suggested: but did not work. Here are the example T1=matrix(1:16, ncol=4) rownames(T1)=rep(c(A,B,C,D)) colnames(T1)=rep(c(X,Y,Z,XX)) T2=matrix(1:9, ncol=3) rownames(T2)=rep(c(A1,B1,C1)) colnames(T2)=rep(c(X,Y,XX)) T1 X Y Z XX A 1 5 9 13 B 2 6 10 14 C 3 7 11 15 D 4 8 12 16 T2 X Y XX A1 1 4 7 B1 2 5 8 C1 3 6 9 T1T2-cbind(T1,T2) Error in cbind(T1, T2) : number of rows of matrices must match (see arg 2) I want to have the following table (probably you noticed that column Z in Table 1 is not in Table 2) X Y XX A 1 5 13 B 2 6 14 C 3 7 15 D 4 8 16 A1 1 4 7 B1 2 5 8 C1 3 6 9 any suggestions? this is just an example, I have very big files (~1700 columns, and ~2500 rows) Kristi CC: r-help@r-project.org From: dwinsem...@comcast.net To: kristi.glo...@hotmail.com Subject: Re: [R] Combine two tables by row with different columns Date: Wed, 9 May 2012 21:47:48 -0400 On May 9, 2012, at 9:21 PM, Kristi Glover wrote: Hi R user, I could not combine two tables. Would any one help me on how I can combine with following example tables? You do realize that tables in R are not dataframes. They are a type of matrix. T1 X Y Z XX A 1 5 9 13 B 2 6 10 14 C 3 7 11 15 D 4 8 12 16 T2 X Y XX a 1 4 7 b 2 5 8 c 3 6 9 I want to get the following table cT X Y XX A 1 5 13 B 2 6 14 C 3 7 15 D 4 8 16 A1 1 4 7 B1 2 5 8 C1 3 6 9 Assuming you will accept the rownames as they stand, then try this: cT - cbind(T1,T2) # works for table, matrices, and dataframes [[alternative HTML version deleted]] Plain text, please. -- David Winsemius, MD West Hartford, CT [[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] Combine two tables by row with different columns
Just omit Z when you combine T1 and T2 -- incidentally, dput() is an even easier way to make a reproducible example (and you don't need rep() for your rownames since you arent repeating anything) T1 - structure(1:16, .Dim = c(4L, 4L), .Dimnames = list(c(A, B, C, D), c(X, Y, Z, XX))) T2 - structure(1:9, .Dim = c(3L, 3L), .Dimnames = list(c(A1, B1, C1), c(X, Y, XX))) It sounds like you want to get the intersection of the colnames for your combined object so something like: cn - intersect(colnames(T1), colnames(T2)) rbind(T1[, cn], T2[, cn]) # I think David misread -- it sounds like you want rbind here Hope this helps, Michael On Wed, May 9, 2012 at 10:04 PM, Kristi Glover kristi.glo...@hotmail.com wrote: Thanks David, I tried 'cbind as you suggested: but did not work. Here are the example T1=matrix(1:16, ncol=4) rownames(T1)=rep(c(A,B,C,D)) colnames(T1)=rep(c(X,Y,Z,XX)) T2=matrix(1:9, ncol=3) rownames(T2)=rep(c(A1,B1,C1)) colnames(T2)=rep(c(X,Y,XX)) T1 X Y Z XX A 1 5 9 13 B 2 6 10 14 C 3 7 11 15 D 4 8 12 16 T2 X Y XX A1 1 4 7 B1 2 5 8 C1 3 6 9 T1T2-cbind(T1,T2) Error in cbind(T1, T2) : number of rows of matrices must match (see arg 2) I want to have the following table (probably you noticed that column Z in Table 1 is not in Table 2) X Y XX A 1 5 13 B 2 6 14 C 3 7 15 D 4 8 16 A1 1 4 7 B1 2 5 8 C1 3 6 9 any suggestions? this is just an example, I have very big files (~1700 columns, and ~2500 rows) Kristi CC: r-help@r-project.org From: dwinsem...@comcast.net To: kristi.glo...@hotmail.com Subject: Re: [R] Combine two tables by row with different columns Date: Wed, 9 May 2012 21:47:48 -0400 On May 9, 2012, at 9:21 PM, Kristi Glover wrote: Hi R user, I could not combine two tables. Would any one help me on how I can combine with following example tables? You do realize that tables in R are not dataframes. They are a type of matrix. T1 X Y Z XX A 1 5 9 13 B 2 6 10 14 C 3 7 11 15 D 4 8 12 16 T2 X Y XX a 1 4 7 b 2 5 8 c 3 6 9 I want to get the following table cT X Y XX A 1 5 13 B 2 6 14 C 3 7 15 D 4 8 16 A1 1 4 7 B1 2 5 8 C1 3 6 9 Assuming you will accept the rownames as they stand, then try this: cT - cbind(T1,T2) # works for table, matrices, and dataframes [[alternative HTML version deleted]] Plain text, please. -- David Winsemius, MD West Hartford, CT [[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] Combine two tables by row with different columns
Hello, Try this T1 - read.table(text= X Y Z XX A 1 5 9 13 B 2 6 10 14 C 3 7 11 15 D 4 8 12 16 , header=TRUE) T2 - read.table(text= X Y XX a 1 4 7 b 2 5 8 c 3 6 9 , header=TRUE) cT - read.table(text= X Y XX A 1 5 13 B 2 6 14 C 3 7 15 D 4 8 16 A1 1 4 7 B1 2 5 8 C1 3 6 9 , header=TRUE) T1; T2; cT (Test - rbind(T1[, colnames(T2)], T2)) # rownames are different, 'Test' has rownames from T2 all.equal(cT, Test) Hope this helps, Rui Barradas Kristi Glover wrote Hi R user, I could not combine two tables. Would any one help me on how I can combine with following example tables? T1 X Y Z XX A 1 5 9 13 B 2 6 10 14 C 3 7 11 15 D 4 8 12 16 T2 X Y XX a 1 4 7 b 2 5 8 c 3 6 9 I want to get the following table cT X Y XX A 1 5 13 B 2 6 14 C 3 7 15 D 4 8 16 A1 1 4 7 B1 2 5 8 C1 3 6 9 Thanks for your help. Thanks, Kristi === [[alternative HTML version deleted]] __ R-help@ mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- View this message in context: http://r.789695.n4.nabble.com/Combine-two-tables-by-row-with-different-columns-tp4622276p4622321.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] Combine variables of different length
Hi, I have got a dataset with the variables Y,X1,X2,X3. Some of these variables contain NAs. Therefore incomplete datasets aren't recognized when I am doing a regression like: model - lm(Y~X1+X2+X3) so the resulting vector of resid(model) is obviousely shorter then the original variables. How can I combine the residuals-vector with the original dataset (Y,Xi,...). I recognize that the residuals give also a kind of an index like from the row names (2,5,7,8,9,12)... Is it possible to use in such a case the data.frame command? Or what is the best way to attach the resulting residuals back to the original dataframe with incomplete datasets? thanks /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] Combine variables of different length
Perhaps something like this: X = jitter(1:10) Y = jitter(3*X-5, factor = 3) X[3] = NA m = lm(Y~X)$fitted.values fits - rep(NA, length(X)); fits[as.numeric(names(m))] - m; cbind(X,Y,fits) Michael On Tue, Nov 1, 2011 at 8:52 AM, Johannes Radinger jradin...@gmx.at wrote: Hi, I have got a dataset with the variables Y,X1,X2,X3. Some of these variables contain NAs. Therefore incomplete datasets aren't recognized when I am doing a regression like: model - lm(Y~X1+X2+X3) so the resulting vector of resid(model) is obviousely shorter then the original variables. How can I combine the residuals-vector with the original dataset (Y,Xi,...). I recognize that the residuals give also a kind of an index like from the row names (2,5,7,8,9,12)... Is it possible to use in such a case the data.frame command? Or what is the best way to attach the resulting residuals back to the original dataframe with incomplete datasets? thanks /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. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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] combine the data frames into comma separated list.
Hi R users, I am new to R and am trying to merge data frames in the following way. Suppose I have n data frames each with two fields. Field 1 is common among data frames but may have different entries. Field 2 is different. Data frame 1: Src Target1 1aaa 1bbb 1ccc 2aaa 3ddd Data frame 2: Src Target2 2 3 4 4 4 Data frame 3: Src Target3 1xx 3yy 5zz 6tt 6uu And so on... I want to convert this into a data frame something similar to: Src Target1 target2 target3 1 aaa,bbb,ccc- xx 2 aaa - 3 ddd yy 4 -,, - 5 - -zz 6 - - tt,uu Basically I am trying to make a consolidated table. Help appreciated. Thanks M - Mary Kindall Yorktown Heights USA [[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.