Re: [R] Dplyr question

2022-06-21 Thread Jeff Newmiller
dplyr != tidyverse.

And questions that presuppose a solution method demonstrate closed-mindedness, 
and reduce the chance that novel solutions will be put forth, or that the 
questioner will actually learn something instead of using the list as a 
glorified search engine.

It is not that answers should avoid contributed packages, but questioners 
should be open to all answers. This is not the tidyverse help list, it is the 
R-help list, and telling people to stay quiet if they cannot conform to the 
biases of the questioner is self-defeating, since the point of the list is to 
learn how problems can be solved. An open mind can learn for instance that 
searching for functions that reshape dataframes in dplyr is doomed and that 
those functions are found elsewhere can use that info to successfully form 
Google queries later without coming back to the list later for the same 
question.

FWIW I use certain tidyverse packages frequently. But solutions using them are 
not always as useful for learning opportunities as base R solutions are.

Oh, and pipes are orthogonal to reshaping... searching for magrittr and 
reshaping isn't a productive association for searches.

On June 21, 2022 4:13:50 PM PDT, Avi Gross via R-help  
wrote:
>Bert and Others,
>
>Now that newer versions of R support a reasonable pipeline method, I think 
>there may be more interest in using functions designed to be easy to use in 
>pipelines, including wrappers that just re-arrange the order for existing 
>functions to make the first argument the one passed along the pipeline.
>
>When people say "dplyr' now it is indeed a specific package but  some use it 
>to mean more like the "tidyverse" group of packages that are meant to operate 
>well together and they includes the "tidyr" package.
>
>The intuitively OBVIOUS solution using base R that is shown is actually a bit 
>restricted and does not trivially scale up to deal with lots more columns that 
>are to be consolidated, perhaps in multiple batches and based on things like 
>suffixes in the names and so on that the tidyverse functions are able to 
>handle. And if it matters, you may want to keep the order of the rows 
>relatively intact and the solution offered does not.
>
>But packages like dplyr are not a full solution and most people would be 
>better off learning all about what core R offers and only supplementing it 
>here and there with selected packages. If you ever have to read code others 
>wrote or modified, ...
>
>In any case, THIS forum seems dedicated for a purpose that precludes more than 
>an aside about packages. Very little that is in packages cannot in theory be 
>done using mostly regular R but I am not sure if that is any longer true or 
>wise. Many packages re-write R functionality as something like much faster 
>code in C or C++ or make use of some R code that is more efficient than you 
>might cobble together on your own. Some is also very general and allows 
>programming at higher levels of abstraction and I specifically include the 
>pipeline methods (now also in R) as such a level of abstraction.
>The topic, loosely, was how to transform your data.frame (or equivalent) from 
>what some call WIDE form to LONG form. That is often done in pipelines where 
>after some steps, the resulting data has to be transformed before being given 
>to a program like one doing graphics with ggplot() and no amount of lecturing 
>suggesting we use native R graphics for everything will in the slightest bit 
>convince me.
>
>So the supplied method, unless suitably placed in a function that takes a 
>data.frame as a first argument and returns the modified new one as a result, 
>will only help for some purposes and be a pain for others as you pause and 
>leave any pipeline to make the change and then ...
>
>As was said, intuitive is fairly meaningless as my personal intuition often 
>intuits multiple ways of looking at something, each one being its own 
>intuitive way and the task often is simply to pick one based on additional 
>factors. It may be intuitively obvious to do it the shortest and easiest way 
>imaginable but also obvious if you will need this again, to make it properly 
>commented and documented and even at times do more error checking or do more 
>general tasks ...
>At MY stage I think I know enough but also see no reason to waste lots of time 
>doing things in many steps with lots of possible mistakes on my part when a 
>few well-coordinated and tested packages make it easy.
>To each their own. But I am NOT suggesting this forum should change, there are 
>others that can accommodate people. And there are way more packages out there 
>that most of us are not even aware of exist!
>
>
>-Original Message-
>From: Bert Gunter 
>To: Rui Barradas 
>Cc: r-help@r-project.org ; Thomas Subia 
>
>Sent: Tue, Jun 21, 2022 2:25 pm
>Subject: Re: [R] Dplyr question
>
>Heh heh. Well "intuitiveness" is in the mind of the intuiter. ;-)
>One might even say that Jeff's and John's 

Re: [R] A Request

2022-06-21 Thread Jim Lemon
Hi Chishti,
Try this:

dim(x)[2]
length(dn)

>From your error message, the two will be different. They should be the
same. A wild guess is that the offending line of code should be:

dimnames[2]<-1:dn

Jim

On Tue, Jun 21, 2022 at 11:10 PM Muhammad Zubair Chishti
 wrote:
>
> Hi, Dear Professor,
> When I run a code in R, I face the following error:
> Error in dimnames(x) <- dn :
>   length of 'dimnames' [2] not equal to array extent
>
> Kindly help how to handle this.
>
> Regards
> Chishti
>
> [[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] Dplyr question

2022-06-21 Thread Rui Barradas

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 > 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
#>                      
#>  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@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
 

Re: [R] Dplyr question

2022-06-21 Thread Jeff Newmiller
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  
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 LocationMeasurement
>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@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] Dplyr question

2022-06-21 Thread Ebert,Timothy Aaron
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  On Behalf Of Thomas Subia
Sent: Tuesday, June 21, 2022 12:23 PM
To: r-help@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  LocationMeasurement
Jun-10 10:34P1A0B0D -0.000208
Jun-10 10:51P1A0B0D -0.000228
Jun-10 11:02P1A0B0D -0.000234
Jun-10 11:17P1A0B0D -0.00022
Jun-10 11:25P1A0B0D -0.000238
Jun-10 10:34P190-90D-0.000195
Jun-10 10:51P190-90D-0.000188
Jun-10 11:02P190-90D-0.000204
Jun-10 11:17P190-90D-0.000205
Jun-10 11:25P190-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=DwIFAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRzsn7AkP-g=2wtqsRoT5-Y52Nm7oSjNuHNQHkSq2Eau1NqsSFofmKxuHYYVfjvnWxMdz4-ihkH_=wC7lz5W60gl0hAYdOMsXi6BiQ7LmX1r-vSkIHY_78IY=
221 Warren Ave, Fremont, CA 94539

"En Dieu nous avons confiance, tous les autres doivent apporter des donnees"

__
R-help@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=DwIFAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRzsn7AkP-g=2wtqsRoT5-Y52Nm7oSjNuHNQHkSq2Eau1NqsSFofmKxuHYYVfjvnWxMdz4-ihkH_=Xb1jmlqPlfkqfb6U6vZjMhHn0dWhTZ_Tj-k4ZHpGXFY=
PLEASE do read the posting guide 
https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html=DwIFAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRzsn7AkP-g=2wtqsRoT5-Y52Nm7oSjNuHNQHkSq2Eau1NqsSFofmKxuHYYVfjvnWxMdz4-ihkH_=D8A_JX8FXGErCjS4rD4k9S200L_F33HHrt0jIKJdRGM=
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] A Request

2022-06-21 Thread Marc Girondot

Hi, you should post a reproducible example if you want to have an answer.

This error is generated when you try to copy an object in another of the 
wrong size.


> a <- data.frame(A=1:2)
> dimnames(a)
[[1]]
[1] "1" "2"

[[2]]
[1] "A"

> dn <- list(c("3", "4"), c("B", "D"))
> dimnames(a) <- dn
Erreur dans `dimnames<-.data.frame`(`*tmp*`, value = list(c("3", "4"), 
c("B",  :

  'dimnames' incorrect pour ce tableau de données

Marc

Le 21/06/2022 à 15:09, Muhammad Zubair Chishti a écrit :

Hi, Dear Professor,
When I run a code in R, I face the following error:
Error in dimnames(x) <- dn :
   length of 'dimnames' [2] not equal to array extent

Kindly help how to handle this.

Regards
Chishti

[[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.


__
Marc Girondot, Pr

Laboratoire Ecologie, Systématique et Evolution
Equipe de Processus Ecologiques et Pressions Anthropiques
CNRS, AgroParisTech et Université Paris-Saclay, UMR 8079
Bâtiment 362
91405 Orsay Cedex, France

Tel:  +33 (0)1.69.15.72.30   Mobile: +33 (0)6.20.18.22.16
e-mail: marc.giron...@universite-paris-saclay.fr
marc.giron...@gmail.com
Web: https://www.ese.universite-paris-saclay.fr/epc/conservation/index.html
Skype: girondot

__
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.