bcrombie
2013-Jul-17 20:12 UTC
[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.
arun
2013-Jul-17 20:41 UTC
[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 <bcrombie at utk.edu> To: r-help at 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 at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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 Alspach
2013-Jul-17 23:59 UTC
[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-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of bcrombie Sent: Thursday, 18 July 2013 8:13 a.m. To: r-help at 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 at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/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}}