Hello,
Right, intuitive is (very) relative. I was thinking of base function
stats::reshape. Its main difficulty is, imho, to reshape to both wide
and long formats. Compared to it, tidyr::pivot_* are (much?) easier to
understand.
Here is a stats::reshape solution.
df_long <- reshape(
data = df1,
idvar = "Time_stamp",
varying = list(2:3),
v.names = "Measurement",
direction = "long")
df_long$time <- sort(names(df1)[-1])[df_long$time]
names(df_long)[2] <- "Location"
df_long
#> Time_stamp Location Measurement
#> Jun-10 10:34.1 Jun-10 10:34 P190-90D -0.000208
#> Jun-10 10:51.1 Jun-10 10:51 P190-90D -0.000228
#> Jun-10 11:02.1 Jun-10 11:02 P190-90D -0.000234
#> Jun-10 11:17.1 Jun-10 11:17 P190-90D -0.000220
#> Jun-10 11:25.1 Jun-10 11:25 P190-90D -0.000238
#> Jun-10 10:34.2 Jun-10 10:34 P1A0B0D -0.000195
#> Jun-10 10:51.2 Jun-10 10:51 P1A0B0D -0.000188
#> Jun-10 11:02.2 Jun-10 11:02 P1A0B0D -0.000204
#> Jun-10 11:17.2 Jun-10 11:17 P1A0B0D -0.000205
#> Jun-10 11:25.2 Jun-10 11:25 P1A0B0D -0.000195
Hope this helps,
Rui Barradas
Às 19:25 de 21/06/2022, Bert Gunter escreveu:
Heh heh. Well "intuitiveness" is in the mind of the intuiter. ;-)
One might even say that Jeff's and John's solutions were the most
"intuitive" as they involved nothing more than the "straightforward"
application of standard base R functionality. (Do note the scare quotes
around 'straightforward'.) Of course, other factors may well be
decisive, such as efficiency, generalizability to the *real* problem and
data, and so forth.
Best to all,
Bert
On Tue, Jun 21, 2022 at 10:50 AM Rui Barradas <ruipbarra...@sapo.pt
<mailto:ruipbarra...@sapo.pt>> wrote:
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
<http://www.fmindustries.com>
> 221 Warren Ave, Fremont, CA 94539
>
> "En Dieu nous avons confiance, tous les autres doivent apporter
des donnees"
>
> ______________________________________________
> R-help@r-project.org <mailto:R-help@r-project.org> mailing list
-- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
<https://stat.ethz.ch/mailman/listinfo/r-help>
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
<http://www.R-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
______________________________________________
R-help@r-project.org <mailto:R-help@r-project.org> mailing list --
To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
<https://stat.ethz.ch/mailman/listinfo/r-help>
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
<http://www.R-project.org/posting-guide.html>
and provide commented, minimal, self-contained, reproducible code.
______________________________________________
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.