[R] Syntax help for 'Pivot_longer'

Kimmo Elo k|mmo@e|o @end|ng |rom utu@||
Sun Nov 28 16:20:38 CET 2021


Hi!

If I understood this correctly, you want to pivot the columns from 2 to
25 and use the first column as a "key". Your data has 27 columns in
total, right?

I tested it with your data and seems that the parameter 'cols' does
have some problems. It takes all variables irrespectively of the range
defined. I tired it with '2:25', '2:10' etc. and always the columns nor
included in 'cols' are present in the long table as separate columns.

Could this be an error in the function? Or can someone from the list
explain this, in my eyes a bit unlogical behaviour...

Best,

Kimmo

su, 2021-11-28 kello 13:57 +0000, Philip Monk kirjoitti:
> Hello,
> 
> I have a wide table that I transform to a long table for analysis.
> The wide table has 25 columns - the first is labels, then columns
> 2:25
> are monthly data of LST which is in 19 rows.
> 
> I mutate this with :
> 
> data_long <- data %>% pivot_longer(cols = 2:25, names_to =
>                                      "Date", values_to = "LST")
> 
> I've decided to add some weather data which might be relevant,
> inputting this as an additional 7 rows of data in the wide format
> (see
> example below of the first 5 months of data).
> 
> I have belatedly realised that I cannot work out how to pivot this
> into the long format I need - the documentation doesn't provide
> enough
> syntax examples for me to work it out (I've not long been using 'R').
> 
> How do I mutate this to provide the additional columns in the long
> table for the weather variables?
> 
> Thanks for your time,
> 
> Philip
> 
> Part-time PhD Student (Environmental Science)
> Lancaster University, UK.
> 
> 
> 
> Wide data
> ------------------
> 
> Buffer            15/01/2010     16/02/2010     20/03/2010
> 24/04/2011      07/05/2010
> 
> 100                6.091741043   6.405879111   8.925945159
> 6.278147269   6.133940129
> 
> 200                5.271975614   5.994054977   7.375445078
> 5.805619599   5.537759202
> 
> 300                4.451891901   5.61142085     6.120095292
> 5.149985946   5.353001442
> 
> 400                3.385694303   4.77953426     5.608927408
> 4.542354226   4.824773827
> 
> 500                2.900508112   4.305900444   5.61367474
> 4.320657374   4.520022189
> 
> 600                3.110238149   3.616699448   4.800003992
> 4.006103489   4.188421662
> 
> 700                 3.150580922   2.848148846   4.216782177
> 3.642003696   3.725611032
> 
> 800                 3.079728958   2.016807672   4.05288041
> 3.315992643   3.278124347
> 
> 900                 2.327902499   1.452876728   3.779922823
> 3.181741995   3.29577819
> 
> 1000               1.641887823   2.120099832   4.267840277
> 3.321634055   3.551965361
> 
> 1100               1.63370882     1.661317381   3.747342619
> 2.814670223   2.807355369
> 
> 1200               0.986559368   1.133219897   2.414025636
> 2.180686348   2.166547946
> 
> 1300               0.920601397   1.237239562   2.647100163
> 2.253223258   2.411947081
> 
> 1400               0.571882394   0.93675648     2.272566024
> 2.07198929     1.954723088
> 
> 1500               0.340505009   0.7379146       2.526476424
> 1.912840489   2.003872651
> 
> 1600               0.813480877   1.026085605   2.643863876
> 1.825988411   2.278799668
> 
> 1700               0.471988882   0.566766122   1.290173713
> 1.360936689   1.45967449
> 
> 1800               0.269067515   0.13349775     0.612263766
> 0.666152106   0.680354177
> 
> 1900               0.303179244   0.082892149   0.465457136
> 0.537232782   0.287185161
> 
> Temperautre   12                    15                     16
>           23                   21.5
> 
> Wind speed     10                    9                      10.5
>           9.5                   9.5
> 
> Wind trend       1                      1                      1
>                 0                     1
> 
> Wind direction  22.5                45                      67.5
>          191.25            56.25
> 
> Humidity           40                   44.5                   22
>               24.5                7
> 
> Pressure          1024               1018.5               1025
>        1005.5            1015.5
> 
> Pressure trend 1                      1                        1
>                1                      1
> 
> 
> 
> 
> long data
> -----------------
> Buffer         Date             LST             Temperature      Wind
> speed ......
> 1                  01.01.21     4                  5                 
>        10
> 2                  01.02.21     5                  2                 
>        11
> 3                  01.03.21     7                  5                 
>        15
> 4                  01.04.21     9                  6                 
>        7
> 5                  01.05.21     7                  5                 
>        10
> 
> ______________________________________________
> R-help using 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.


More information about the R-help mailing list