Colleagues: The header of my data set is: Time_stamp P1A0B0D P190-90D Jun-10 10:34 -0.000208 -0.000195 Jun-10 10:51 -0.000228 -0.000188 Jun-10 11:02 -0.000234 -0.000204 Jun-10 11:17 -0.00022 -0.000205 Jun-10 11:25 -0.000238 -0.000195 I want my data set to resemble: Time_stamp Location Measurement Jun-10 10:34 P1A0B0D -0.000208 Jun-10 10:51 P1A0B0D -0.000228 Jun-10 11:02 P1A0B0D -0.000234 Jun-10 11:17 P1A0B0D -0.00022 Jun-10 11:25 P1A0B0D -0.000238 Jun-10 10:34 P190-90D -0.000195 Jun-10 10:51 P190-90D -0.000188 Jun-10 11:02 P190-90D -0.000204 Jun-10 11:17 P190-90D -0.000205 Jun-10 11:25 P190-90D -0.000195 I need some advice on how to do this using dplyr. V/R Thomas Subia FM Industries, Inc. - NGK Electronics, USA | www.fmindustries.com 221 Warren Ave, Fremont, CA 94539 "En Dieu nous avons confiance, tous les autres doivent apporter des donnees"
I do not see how to do this with exclusively dplyr commands. I might be able to make it work by including commands outside of dplyr, but at that point I would use pivot_longer() from the tidyr package. Tim -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of Thomas Subia Sent: Tuesday, June 21, 2022 12:23 PM To: r-help at r-project.org Subject: [R] Dplyr question [External Email] Colleagues: The header of my data set is: Time_stamp P1A0B0D P190-90D Jun-10 10:34 -0.000208 -0.000195 Jun-10 10:51 -0.000228 -0.000188 Jun-10 11:02 -0.000234 -0.000204 Jun-10 11:17 -0.00022 -0.000205 Jun-10 11:25 -0.000238 -0.000195 I want my data set to resemble: Time_stamp Location Measurement Jun-10 10:34 P1A0B0D -0.000208 Jun-10 10:51 P1A0B0D -0.000228 Jun-10 11:02 P1A0B0D -0.000234 Jun-10 11:17 P1A0B0D -0.00022 Jun-10 11:25 P1A0B0D -0.000238 Jun-10 10:34 P190-90D -0.000195 Jun-10 10:51 P190-90D -0.000188 Jun-10 11:02 P190-90D -0.000204 Jun-10 11:17 P190-90D -0.000205 Jun-10 11:25 P190-90D -0.000195 I need some advice on how to do this using dplyr. V/R Thomas Subia FM Industries, Inc. - NGK Electronics, USA | https://urldefense.proofpoint.com/v2/url?u=http-3A__www.fmindustries.com&d=DwIFAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=2wtqsRoT5-Y52Nm7oSjNuHNQHkSq2Eau1NqsSFofmKxuHYYVfjvnWxMdz4-ihkH_&s=wC7lz5W60gl0hAYdOMsXi6BiQ7LmX1r-vSkIHY_78IY&e221 Warren Ave, Fremont, CA 94539 "En Dieu nous avons confiance, tous les autres doivent apporter des donnees" ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp&d=DwIFAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=2wtqsRoT5-Y52Nm7oSjNuHNQHkSq2Eau1NqsSFofmKxuHYYVfjvnWxMdz4-ihkH_&s=Xb1jmlqPlfkqfb6U6vZjMhHn0dWhTZ_Tj-k4ZHpGXFY&ePLEASE do read the posting guide https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwIFAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=2wtqsRoT5-Y52Nm7oSjNuHNQHkSq2Eau1NqsSFofmKxuHYYVfjvnWxMdz4-ihkH_&s=D8A_JX8FXGErCjS4rD4k9S200L_F33HHrt0jIKJdRGM&eand provide commented, minimal, self-contained, reproducible code.
Thomas, Your need is quite common and base R has ways to do it too. This forum supposedly?focuses on more basic base R. I am sure someone will gladly tell you how to do it?that way. For dplyr just go long: https://tidyr.tidyverse.org/reference/pivot_longer.html The vignette?may be an easier read: https://tidyr.tidyverse.org/articles/pivot.html -----Original Message----- From: Thomas Subia <thomas.subia at fmindustries.com> To: r-help at r-project.org <r-help at r-project.org> Sent: Tue, Jun 21, 2022 12:22 pm Subject: [R] Dplyr question Colleagues: The header of my data set is: Time_stamp??? P1A0B0D??? P190-90D Jun-10 10:34??? -0.000208??? -0.000195 Jun-10 10:51??? -0.000228??? -0.000188 Jun-10 11:02??? -0.000234??? -0.000204 Jun-10 11:17??? -0.00022??? -0.000205 Jun-10 11:25??? -0.000238??? -0.000195 I want my data set to resemble: Time_stamp??? Location??? Measurement Jun-10 10:34??? P1A0B0D??? -0.000208 Jun-10 10:51??? P1A0B0D??? -0.000228 Jun-10 11:02??? P1A0B0D??? -0.000234 Jun-10 11:17??? P1A0B0D??? -0.00022 Jun-10 11:25??? P1A0B0D??? -0.000238 Jun-10 10:34??? P190-90D??? -0.000195 Jun-10 10:51??? P190-90D??? -0.000188 Jun-10 11:02??? P190-90D??? -0.000204 Jun-10 11:17??? P190-90D??? -0.000205 Jun-10 11:25??? P190-90D??? -0.000195 I need some advice on how to do this using dplyr. V/R Thomas Subia FM Industries, Inc. - NGK Electronics, USA | www.fmindustries.com 221 Warren Ave, Fremont, CA 94539 "En Dieu nous avons confiance, tous les autres doivent apporter des donnees" ______________________________________________ R-help at 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]]
Beware of being too specific about how you want something solved... not just here, but in all contexts. Your question is like "how do I slice this apple with this potholder"... dplyr actually doesn't do that, and you can benefit from learning how to do things in general, not just in your preferred idiom. ans <- do.call( rbind , lapply( names(dta[ -1 ]) , function( nm ) data.frame( dta[1] , Location = nm , Measurement = dta[[nm]] , stringsAsFactors = FALSE ) ) ) On June 21, 2022 9:22:48 AM PDT, Thomas Subia <thomas.subia at fmindustries.com> wrote:>Colleagues: > >The header of my data set is: >Time_stamp P1A0B0D P190-90D >Jun-10 10:34 -0.000208 -0.000195 >Jun-10 10:51 -0.000228 -0.000188 >Jun-10 11:02 -0.000234 -0.000204 >Jun-10 11:17 -0.00022 -0.000205 >Jun-10 11:25 -0.000238 -0.000195 > >I want my data set to resemble: > >Time_stamp Location Measurement >Jun-10 10:34 P1A0B0D -0.000208 >Jun-10 10:51 P1A0B0D -0.000228 >Jun-10 11:02 P1A0B0D -0.000234 >Jun-10 11:17 P1A0B0D -0.00022 >Jun-10 11:25 P1A0B0D -0.000238 >Jun-10 10:34 P190-90D -0.000195 >Jun-10 10:51 P190-90D -0.000188 >Jun-10 11:02 P190-90D -0.000204 >Jun-10 11:17 P190-90D -0.000205 >Jun-10 11:25 P190-90D -0.000195 > >I need some advice on how to do this using dplyr. > >V/R >Thomas Subia > >FM Industries, Inc. - NGK Electronics, USA | www.fmindustries.com >221 Warren Ave, Fremont, CA 94539 > >"En Dieu nous avons confiance, tous les autres doivent apporter des donnees" > >______________________________________________ >R-help at 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.
Hello, pivot_longer is a package tidyr function, not dplyr. I find its syntax very intuitive. Here is a solution. x <- "Time_stamp P1A0B0D P190-90D 'Jun-10 10:34' -0.000208 -0.000195 'Jun-10 10:51' -0.000228 -0.000188 'Jun-10 11:02' -0.000234 -0.000204 'Jun-10 11:17' -0.00022 -0.000205 'Jun-10 11:25' -0.000238 -0.000195" df1 <- read.table(textConnection(x), header = TRUE, check.names = FALSE) suppressPackageStartupMessages({ library(dplyr) library(tidyr) }) df1 %>% pivot_longer( cols = -Time_stamp, # or starts_with("P1") names_to = "Location", values_to = "Measurement" ) %>% arrange(desc(Location), Time_stamp) #> # A tibble: 10 ? 3 #> Time_stamp Location Measurement #> <chr> <chr> <dbl> #> 1 Jun-10 10:34 P1A0B0D -0.000208 #> 2 Jun-10 10:51 P1A0B0D -0.000228 #> 3 Jun-10 11:02 P1A0B0D -0.000234 #> 4 Jun-10 11:17 P1A0B0D -0.00022 #> 5 Jun-10 11:25 P1A0B0D -0.000238 #> 6 Jun-10 10:34 P190-90D -0.000195 #> 7 Jun-10 10:51 P190-90D -0.000188 #> 8 Jun-10 11:02 P190-90D -0.000204 #> 9 Jun-10 11:17 P190-90D -0.000205 #> 10 Jun-10 11:25 P190-90D -0.000195 Hope this helps, Rui Barradas ?s 17:22 de 21/06/2022, Thomas Subia escreveu:> Colleagues: > > The header of my data set is: > Time_stamp P1A0B0D P190-90D > Jun-10 10:34 -0.000208 -0.000195 > Jun-10 10:51 -0.000228 -0.000188 > Jun-10 11:02 -0.000234 -0.000204 > Jun-10 11:17 -0.00022 -0.000205 > Jun-10 11:25 -0.000238 -0.000195 > > I want my data set to resemble: > > Time_stamp Location Measurement > Jun-10 10:34 P1A0B0D -0.000208 > Jun-10 10:51 P1A0B0D -0.000228 > Jun-10 11:02 P1A0B0D -0.000234 > Jun-10 11:17 P1A0B0D -0.00022 > Jun-10 11:25 P1A0B0D -0.000238 > Jun-10 10:34 P190-90D -0.000195 > Jun-10 10:51 P190-90D -0.000188 > Jun-10 11:02 P190-90D -0.000204 > Jun-10 11:17 P190-90D -0.000205 > Jun-10 11:25 P190-90D -0.000195 > > I need some advice on how to do this using dplyr. > > V/R > Thomas Subia > > FM Industries, Inc. - NGK Electronics, USA | www.fmindustries.com > 221 Warren Ave, Fremont, CA 94539 > > "En Dieu nous avons confiance, tous les autres doivent apporter des donnees" > > ______________________________________________ > R-help at 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.
Why do you want to use dplyr? It's easy using base R. original <- ... a <- cbind(original[,-3], Location=colnames(original)[2]) colnames(a)[2] <- "Measurement" b <- cbind(original[,-2], Location=colnames(original)[3]) colnames(b)[2] <- "Measurement" result <- rbind(a, b)[,c(1,3,2)] On Wed, 22 Jun 2022 at 04:23, Thomas Subia <thomas.subia at fmindustries.com> wrote:> Colleagues: > > The header of my data set is: > Time_stamp P1A0B0D P190-90D > Jun-10 10:34 -0.000208 -0.000195 > Jun-10 10:51 -0.000228 -0.000188 > Jun-10 11:02 -0.000234 -0.000204 > Jun-10 11:17 -0.00022 -0.000205 > Jun-10 11:25 -0.000238 -0.000195 > > I want my data set to resemble: > > Time_stamp Location Measurement > Jun-10 10:34 P1A0B0D -0.000208 > Jun-10 10:51 P1A0B0D -0.000228 > Jun-10 11:02 P1A0B0D -0.000234 > Jun-10 11:17 P1A0B0D -0.00022 > Jun-10 11:25 P1A0B0D -0.000238 > Jun-10 10:34 P190-90D -0.000195 > Jun-10 10:51 P190-90D -0.000188 > Jun-10 11:02 P190-90D -0.000204 > Jun-10 11:17 P190-90D -0.000205 > Jun-10 11:25 P190-90D -0.000195 > > I need some advice on how to do this using dplyr. > > V/R > Thomas Subia > > FM Industries, Inc. - NGK Electronics, USA | www.fmindustries.com > 221 Warren Ave, Fremont, CA 94539 > > "En Dieu nous avons confiance, tous les autres doivent apporter des > donnees" > > ______________________________________________ > R-help at 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]]