Re: [R] split a factor into single elements

2024-03-28 Thread Duncan Murdoch

On 28/03/2024 7:48 a.m., Stefano Sofia wrote:

as.factor(2024, 12, 1, 0, 0)


That doesn't work.  You need to put the numbers in a single vector as 
Fabio did, or you'll see this:


Error in as.factor(2024, 12, 1, 0, 0) : unused arguments (12, 1, 0, 0)

Duncan Murdoch

__
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] split a factor into single elements

2024-03-28 Thread Ebert,Timothy Aaron
Here are some pieces of working code. I assume you want the second one or the 
third one that is functionally the same but all in one statement. I do not 
understand why it is a factor, but I will assume that there is a current and 
future reason for that. This means I cannot alter the string_data variable, or 
you can simplify by not making the variable a factor only to turn it back into 
character.

mydf <- data.frame(id_station = 1234, string_data = c(2024, 12, 1, 0, 0), 
rainfall_value= 55)
mydf$string_data <- as.factor(mydf$string_data)

mydf <- data.frame(id_station = 1234, string_data = "2024, 12, 1, 0, 0", 
rainfall_value= 55)
mydf$string_data <- as.factor(mydf$string_data)

mydf <- data.frame(id_station = 1234, string_data = as.factor("2024, 12, 1, 0, 
0"), rainfall_value= 55)

mydf <- data.frame(id_station = 1234, string_data = as.factor("2024, 12, 1, 0, 
0"), rainfall_value= 55)
mydf$string_data2 <- as.character(mydf$string_data)

#I assume there are many records in the data frame and your example is for 
demonstration only.
#I cannot assume that all records are the same, though you may be able to 
simplify if that is true.
#Split the string based on commas.
split_values <- strsplit(mydf$string_data2, ",")

# find the maximum string length
max_length <- max(lengths(split_values))

# Add new variables to the data frame
for (i in 1:max_length) {
  new_var_name <- paste0("VAR_", i)
  mydf[[new_var_name]] <- sapply(split_values, function(x) ifelse(length(x) >= 
i, x[i], NA))
}

# Convert to numeric
 for (i in 1:max_length) {
   new_var_name <- paste0("VAR_", i)
   mydf[[new_var_name]] <- as.numeric(mydf[[new_var_name]])
 }
# remove trash
mydf <- mydf[,-4]
# Provide more useful names
colnames(mydf) <- c("id_station", "string_data", "rainfall_mm", "Year", 
"Month", "Day", "hour", "minute")

Regards,
Tim

-Original Message-
From: R-help  On Behalf Of Stefano Sofia
Sent: Thursday, March 28, 2024 7:48 AM
To: Fabio D'Agostino ; r-help@R-project.org
Subject: Re: [R] split a factor into single elements

[External Email]

Sorry for my hurry.

The correct reproducible code is different from the initial one. The correct 
example is


mydf <- data.frame(id_station = 1234, string_data = as.factor(2024, 12, 1, 0, 
0), rainfall_value= 55)


In this case mydf$string_data is a factor, but of length 1 (and not 5 like in 
the initial example).

Therefore the suggestion offered by Fabio does not work.


Any suggestion?

Sorry again for my mistake

Stefano



 (oo)
--oOO--( )--OOo--
Stefano Sofia PhD
Civil Protection - Marche Region - Italy Meteo Section Snow Section Via del 
Colle Ameno 5
60126 Torrette di Ancona, Ancona (AN)
Uff: +39 071 806 7743
E-mail: stefano.so...@regione.marche.it
---Oo-oO



Da: Fabio D'Agostino 
Inviato: gioved  28 marzo 2024 12:20
A: Stefano Sofia; r-help@R-project.org
Oggetto: Re: [R] split a factor into single elements


Non si ricevono spesso messaggi di posta elettronica da 
dagostinof...@gmail.com. Informazioni sul perch
importante

Hi Stefano,
maybe something like this can help you?

myfactor <- as.factor(c(2024, 2, 1, 0, 0))

# Convert factor values to integers
first_element <- as.integer(as.character(myfactor)[1])
second_element <- as.integer(as.character(myfactor)[2])
third_element <- as.integer(as.character(myfactor)[3])

# Print the results
first_element
[1] 2024
second_element
[1] 2
third_element
[1] 1

# Check the type of the object
typeof(first_element)
[1] "integer"

Fabio

Il giorno gio 28 mar 2024 alle ore 11:29 Stefano Sofia 
mailto:stefano.so...@regione.marche.it>> ha 
scritto:
Dear R-list users,

forgive me for this silly question, I did my best to find a solution with no 
success.

Suppose I have a factor type like


myfactor <- as.factor(2024, 2, 1, 0, 0)


There are no characters (and therefore strsplit for eample does not work).

I need to store separately the 1st, 2nd and 3rd elements as integers. How can I 
do?


Thank you for your help

Stefano


 (oo)
--oOO--( )--OOo--
Stefano Sofia PhD
Civil Protection - Marche Region - Italy Meteo Section Snow Section Via del 
Colle Ameno 5
60126 Torrette di Ancona, Ancona (AN)
Uff: +39 071 806 7743
E-mail: stefano.so...@regione.marche.it
---Oo-oO



AVVISO IMPORTANTE: Questo messaggio di posta elettronica pu  contenere 
informazioni confidenziali, pertanto   destinato solo a persone autorizzate 
alla ricezione. I messaggi di posta elettronica per i client di Regione Marche 
possono contenere informazioni confidenziali e con privilegi legali. Se non si  
 il destinatario specificato, non leggere, copiare, inoltrare o archiviare 
questo messaggio. Se si   ricevuto questo messaggio per 

Re: [R] split a factor into single elements

2024-03-28 Thread Fabio D'Agostino
Hi Stefano,
maybe something like this can help you?

myfactor <- as.factor(c(2024, 2, 1, 0, 0))

# Convert factor values to integers
first_element <- as.integer(as.character(myfactor)[1])
second_element <- as.integer(as.character(myfactor)[2])
third_element <- as.integer(as.character(myfactor)[3])

# Print the results
first_element
[1] 2024
second_element
[1] 2
third_element
[1] 1

# Check the type of the object
typeof(first_element)
[1] "integer"

Fabio

Il giorno gio 28 mar 2024 alle ore 11:29 Stefano Sofia <
stefano.so...@regione.marche.it> ha scritto:

> Dear R-list users,
>
> forgive me for this silly question, I did my best to find a solution with
> no success.
>
> Suppose I have a factor type like
>
>
> myfactor <- as.factor(2024, 2, 1, 0, 0)
>
>
> There are no characters (and therefore strsplit for eample does not work).
>
> I need to store separately the 1st, 2nd and 3rd elements as integers. How
> can I do?
>
>
> Thank you for your help
>
> Stefano
>
>
>  (oo)
> --oOO--( )--OOo--
> Stefano Sofia PhD
> Civil Protection - Marche Region - Italy
> Meteo Section
> Snow Section
> Via del Colle Ameno 5
> 60126 Torrette di Ancona, Ancona (AN)
> Uff: +39 071 806 7743
> E-mail: stefano.so...@regione.marche.it
> ---Oo-oO
>
> 
>
> AVVISO IMPORTANTE: Questo messaggio di posta elettronica può contenere
> informazioni confidenziali, pertanto è destinato solo a persone autorizzate
> alla ricezione. I messaggi di posta elettronica per i client di Regione
> Marche possono contenere informazioni confidenziali e con privilegi legali.
> Se non si è il destinatario specificato, non leggere, copiare, inoltrare o
> archiviare questo messaggio. Se si è ricevuto questo messaggio per errore,
> inoltrarlo al mittente ed eliminarlo completamente dal sistema del proprio
> computer. Ai sensi dell'art. 6 della DGR n. 1394/2008 si segnala che, in
> caso di necessità ed urgenza, la risposta al presente messaggio di posta
> elettronica può essere visionata da persone estranee al destinatario.
> IMPORTANT NOTICE: This e-mail message is intended to be received only by
> persons entitled to receive the confidential information it may contain.
> E-mail messages to clients of Regione Marche may contain information that
> is confidential and legally privileged. Please do not read, copy, forward,
> or store this message unless you are an intended recipient of it. If you
> have received this message in error, please forward it to the sender and
> delete it completely from your computer system.
>
> [[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.
>

[[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] split a factor into single elements

2024-03-28 Thread Stefano Sofia
Dear R-list users,

forgive me for this silly question, I did my best to find a solution with no 
success.

Suppose I have a factor type like


myfactor <- as.factor(2024, 2, 1, 0, 0)


There are no characters (and therefore strsplit for eample does not work).

I need to store separately the 1st, 2nd and 3rd elements as integers. How can I 
do?


Thank you for your help

Stefano


 (oo)
--oOO--( )--OOo--
Stefano Sofia PhD
Civil Protection - Marche Region - Italy
Meteo Section
Snow Section
Via del Colle Ameno 5
60126 Torrette di Ancona, Ancona (AN)
Uff: +39 071 806 7743
E-mail: stefano.so...@regione.marche.it
---Oo-oO



AVVISO IMPORTANTE: Questo messaggio di posta elettronica pu� contenere 
informazioni confidenziali, pertanto � destinato solo a persone autorizzate 
alla ricezione. I messaggi di posta elettronica per i client di Regione Marche 
possono contenere informazioni confidenziali e con privilegi legali. Se non si 
� il destinatario specificato, non leggere, copiare, inoltrare o archiviare 
questo messaggio. Se si � ricevuto questo messaggio per errore, inoltrarlo al 
mittente ed eliminarlo completamente dal sistema del proprio computer. Ai sensi 
dell'art. 6 della DGR n. 1394/2008 si segnala che, in caso di necessit� ed 
urgenza, la risposta al presente messaggio di posta elettronica pu� essere 
visionata da persone estranee al destinatario.
IMPORTANT NOTICE: This e-mail message is intended to be received only by 
persons entitled to receive the confidential information it may contain. E-mail 
messages to clients of Regione Marche may contain information that is 
confidential and legally privileged. Please do not read, copy, forward, or 
store this message unless you are an intended recipient of it. If you have 
received this message in error, please forward it to the sender and delete it 
completely from your computer system.

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


Re: [R] split a factor into single elements

2024-03-28 Thread Stefano Sofia
Sorry for my hurry.

The correct reproducible code is different from the initial one. The correct 
example is


mydf <- data.frame(id_station = 1234, string_data = as.factor(2024, 12, 1, 0, 
0), rainfall_value= 55)


In this case mydf$string_data is a factor, but of length 1 (and not 5 like in 
the initial example).

Therefore the suggestion offered by Fabio does not work.


Any suggestion?

Sorry again for my mistake

Stefano



 (oo)
--oOO--( )--OOo--
Stefano Sofia PhD
Civil Protection - Marche Region - Italy
Meteo Section
Snow Section
Via del Colle Ameno 5
60126 Torrette di Ancona, Ancona (AN)
Uff: +39 071 806 7743
E-mail: stefano.so...@regione.marche.it
---Oo-oO



Da: Fabio D'Agostino 
Inviato: gioved� 28 marzo 2024 12:20
A: Stefano Sofia; r-help@R-project.org
Oggetto: Re: [R] split a factor into single elements


Non si ricevono spesso messaggi di posta elettronica da 
dagostinof...@gmail.com. Informazioni sul perch� � 
importante

Hi Stefano,
maybe something like this can help you?

myfactor <- as.factor(c(2024, 2, 1, 0, 0))

# Convert factor values to integers
first_element <- as.integer(as.character(myfactor)[1])
second_element <- as.integer(as.character(myfactor)[2])
third_element <- as.integer(as.character(myfactor)[3])

# Print the results
first_element
[1] 2024
second_element
[1] 2
third_element
[1] 1

# Check the type of the object
typeof(first_element)
[1] "integer"

Fabio

Il giorno gio 28 mar 2024 alle ore 11:29 Stefano Sofia 
mailto:stefano.so...@regione.marche.it>> ha 
scritto:
Dear R-list users,

forgive me for this silly question, I did my best to find a solution with no 
success.

Suppose I have a factor type like


myfactor <- as.factor(2024, 2, 1, 0, 0)


There are no characters (and therefore strsplit for eample does not work).

I need to store separately the 1st, 2nd and 3rd elements as integers. How can I 
do?


Thank you for your help

Stefano


 (oo)
--oOO--( )--OOo--
Stefano Sofia PhD
Civil Protection - Marche Region - Italy
Meteo Section
Snow Section
Via del Colle Ameno 5
60126 Torrette di Ancona, Ancona (AN)
Uff: +39 071 806 7743
E-mail: stefano.so...@regione.marche.it
---Oo-oO



AVVISO IMPORTANTE: Questo messaggio di posta elettronica pu� contenere 
informazioni confidenziali, pertanto � destinato solo a persone autorizzate 
alla ricezione. I messaggi di posta elettronica per i client di Regione Marche 
possono contenere informazioni confidenziali e con privilegi legali. Se non si 
� il destinatario specificato, non leggere, copiare, inoltrare o archiviare 
questo messaggio. Se si � ricevuto questo messaggio per errore, inoltrarlo al 
mittente ed eliminarlo completamente dal sistema del proprio computer. Ai sensi 
dell'art. 6 della DGR n. 1394/2008 si segnala che, in caso di necessit� ed 
urgenza, la risposta al presente messaggio di posta elettronica pu� essere 
visionata da persone estranee al destinatario.
IMPORTANT NOTICE: This e-mail message is intended to be received only by 
persons entitled to receive the confidential information it may contain. E-mail 
messages to clients of Regione Marche may contain information that is 
confidential and legally privileged. Please do not read, copy, forward, or 
store this message unless you are an intended recipient of it. If you have 
received this message in error, please forward it to the sender and delete it 
completely from your computer system.

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



AVVISO IMPORTANTE: Questo messaggio di posta 

Re: [R] split a factor into single elements

2024-03-28 Thread Stefano Sofia
Thank you Fabio.

So easy and straighforward!


Stefano


 (oo)
--oOO--( )--OOo--
Stefano Sofia PhD
Civil Protection - Marche Region - Italy
Meteo Section
Snow Section
Via del Colle Ameno 5
60126 Torrette di Ancona, Ancona (AN)
Uff: +39 071 806 7743
E-mail: stefano.so...@regione.marche.it
---Oo-oO



Da: Fabio D'Agostino 
Inviato: gioved� 28 marzo 2024 12:20
A: Stefano Sofia; r-help@R-project.org
Oggetto: Re: [R] split a factor into single elements


Non si ricevono spesso messaggi di posta elettronica da 
dagostinof...@gmail.com. Informazioni sul perch� � 
importante

Hi Stefano,
maybe something like this can help you?

myfactor <- as.factor(c(2024, 2, 1, 0, 0))

# Convert factor values to integers
first_element <- as.integer(as.character(myfactor)[1])
second_element <- as.integer(as.character(myfactor)[2])
third_element <- as.integer(as.character(myfactor)[3])

# Print the results
first_element
[1] 2024
second_element
[1] 2
third_element
[1] 1

# Check the type of the object
typeof(first_element)
[1] "integer"

Fabio

Il giorno gio 28 mar 2024 alle ore 11:29 Stefano Sofia 
mailto:stefano.so...@regione.marche.it>> ha 
scritto:
Dear R-list users,

forgive me for this silly question, I did my best to find a solution with no 
success.

Suppose I have a factor type like


myfactor <- as.factor(2024, 2, 1, 0, 0)


There are no characters (and therefore strsplit for eample does not work).

I need to store separately the 1st, 2nd and 3rd elements as integers. How can I 
do?


Thank you for your help

Stefano


 (oo)
--oOO--( )--OOo--
Stefano Sofia PhD
Civil Protection - Marche Region - Italy
Meteo Section
Snow Section
Via del Colle Ameno 5
60126 Torrette di Ancona, Ancona (AN)
Uff: +39 071 806 7743
E-mail: stefano.so...@regione.marche.it
---Oo-oO



AVVISO IMPORTANTE: Questo messaggio di posta elettronica pu� contenere 
informazioni confidenziali, pertanto � destinato solo a persone autorizzate 
alla ricezione. I messaggi di posta elettronica per i client di Regione Marche 
possono contenere informazioni confidenziali e con privilegi legali. Se non si 
� il destinatario specificato, non leggere, copiare, inoltrare o archiviare 
questo messaggio. Se si � ricevuto questo messaggio per errore, inoltrarlo al 
mittente ed eliminarlo completamente dal sistema del proprio computer. Ai sensi 
dell'art. 6 della DGR n. 1394/2008 si segnala che, in caso di necessit� ed 
urgenza, la risposta al presente messaggio di posta elettronica pu� essere 
visionata da persone estranee al destinatario.
IMPORTANT NOTICE: This e-mail message is intended to be received only by 
persons entitled to receive the confidential information it may contain. E-mail 
messages to clients of Regione Marche may contain information that is 
confidential and legally privileged. Please do not read, copy, forward, or 
store this message unless you are an intended recipient of it. If you have 
received this message in error, please forward it to the sender and delete it 
completely from your computer system.

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



AVVISO IMPORTANTE: Questo messaggio di posta elettronica pu� contenere 
informazioni confidenziali, pertanto � destinato solo a persone autorizzate 
alla ricezione. I messaggi di posta elettronica per i client di Regione Marche 
possono contenere informazioni confidenziali e con privilegi legali. Se non si 
� il destinatario specificato, non leggere, copiare, inoltrare o archiviare 
questo messaggio. Se si � ricevuto 

Re: [R] Output of tapply function as data frame

2024-03-28 Thread Ogbos Okike
Dear Deepayan,
Thanks for your kind response.
Regards
Ogbos

On Thu, Mar 28, 2024 at 3:40 AM Deepayan Sarkar 
wrote:

> For more complicated examples, the (relatively new) array2DF()
> function is also useful:
>
> > with(data, tapply(count, Date, mean)) |> array2DF()
> Var1Value
> 1 2024-03-23 5.416667
> 2 2024-03-24 5.50
> 3 2024-03-25 6.00
> 4 2024-03-26 4.476190
> 5 2024-03-27 6.538462
> 6 2024-03-28 5.20
>
> or
>
> > tapply(data, ~ Date, with, mean(count)) |> array2DF(responseName =
> "count")
> Datecount
> 1 2024-03-23 5.416667
> 2 2024-03-24 5.50
> 3 2024-03-25 6.00
> 4 2024-03-26 4.476190
> 5 2024-03-27 6.538462
> 6 2024-03-28 5.20
>
> Best,
> -Deepayan
>
> On Wed, 27 Mar 2024 at 13:15, Rui Barradas  wrote:
> >
> > Às 04:30 de 27/03/2024, Ogbos Okike escreveu:
> > > Warm greetings to you all.
> > >
> > > Using the tapply function below:
> > > data<-read.table("FD1month",col.names = c("Dates","count"))
> > > x=data$count
> > >   f<-factor(data$Dates)
> > > AB<- tapply(x,f,mean)
> > >
> > >
> > > I made a simple calculation. The result, stored in AB, is of the form
> > > below. But an effort to write AB to a file as a data frame fails. When
> I
> > > use the write table, it only produces the count column and strip of the
> > > first column (date).
> > >
> > > 2005-11-01 2005-12-01 2006-01-01 2006-02-01 2006-03-01 2006-04-01
> > > 2006-05-01
> > >   -4.106887  -4.259154  -5.836090  -4.756757  -4.118011  -4.487942
> > >   -4.430705
> > > 2006-06-01 2006-07-01 2006-08-01 2006-09-01 2006-10-01 2006-11-01
> > > 2006-12-01
> > >   -3.856727  -6.067103  -6.418767  -4.383031  -3.985805  -4.768196
> > > -10.072579
> > > 2007-01-01 2007-02-01 2007-03-01 2007-04-01 2007-05-01 2007-06-01
> > > 2007-07-01
> > >   -5.342338  -4.653128  -4.325094  -4.525373  -4.574783  -3.915600
> > >   -4.127980
> > > 2007-08-01 2007-09-01 2007-10-01 2007-11-01 2007-12-01 2008-01-01
> > > 2008-02-01
> > >   -3.952150  -4.033518  -4.532878  -4.522941  -4.485693  -3.922155
> > >   -4.183578
> > > 2008-03-01 2008-04-01 2008-05-01 2008-06-01 2008-07-01 2008-08-01
> > > 2008-09-01
> > >   -4.336969  -3.813306  -4.296579  -4.575095  -4.036036  -4.727994
> > >   -4.347428
> > > 2008-10-01 2008-11-01 2008-12-01
> > >   -4.029918  -4.260326  -4.454224
> > >
> > > But the normal format I wish to display only appears on the terminal,
> > > leading me to copy it and paste into a text file. That is, when I
> enter AB
> > > on the terminal, it returns a format in the form:
> > >
> > > 008-02-01  -4.183578
> > > 2008-03-01  -4.336969
> > > 2008-04-01  -3.813306
> > > 2008-05-01  -4.296579
> > > 2008-06-01  -4.575095
> > > 2008-07-01  -4.036036
> > > 2008-08-01  -4.727994
> > > 2008-09-01  -4.347428
> > > 2008-10-01  -4.029918
> > > 2008-11-01  -4.260326
> > > 2008-12-01  -4.454224
> > >
> > > Now, my question: How do I write out two columns displayed by AB on the
> > > terminal to a file?
> > >
> > > I have tried using AB<-data.frame(AB) but it doesn't work either.
> > >
> > > Many thanks for your time.
> > > Ogbos
> > >
> > >   [[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.
> > Hello,
> >
> > The main trick is to pipe to as.data.frame. But the result will have one
> > column only, you must assign the dates from the df's row names.
> > I also include an aggregate solution.
> >
> >
> >
> > # create a test data set
> > set.seed(2024)
> > data <- data.frame(
> >Date = sample(seq(Sys.Date() - 5, Sys.Date(), by = "1 days"), 100L,
> > TRUE),
> >count = sample(10L, 100L, TRUE)
> > )
> >
> > # coerce tapply's result to class "data.frame"
> > res <- with(data, tapply(count, Date, mean)) |> as.data.frame()
> > # assign a dates column from the row names
> > res$Date <- row.names(res)
> > # cosmetics
> > names(res)[2:1] <- names(data)
> > # note that the row names are still tapply's names vector
> > # and that the columns order is not Date/count. Both are fixed
> > # after the calculations.
> > res
> > #>   count   Date
> > #> 2024-03-22 5.416667 2024-03-22
> > #> 2024-03-23 5.50 2024-03-23
> > #> 2024-03-24 6.00 2024-03-24
> > #> 2024-03-25 4.476190 2024-03-25
> > #> 2024-03-26 6.538462 2024-03-26
> > #> 2024-03-27 5.20 2024-03-27
> >
> > # fix the columns' order
> > res <- res[2:1]
> >
> >
> >
> > # better all in one instruction
> > aggregate(count ~ Date, data, mean)
> > #> Datecount
> > #> 1 2024-03-22 5.416667
> > #> 2 2024-03-23 5.50
> > #> 3 2024-03-24 6.00
> > #> 4 2024-03-25 4.476190
> > #> 5 2024-03-26 6.538462
> > #> 6 2024-03-27 5.20
> >
> >
> >
> > Also,
> > I'm glad to help as always but Ogbos, you have been 

Re: [R] Output of tapply function as data frame: Problem Fixed

2024-03-28 Thread Ogbos Okike
Dear Rui,
Thanks again for resolving this. I have already started using the version
that works for me.

But to clarify the second part, please let me paste the what I did and the
error message:

> set.seed(2024)
> data <- data.frame(
+Date = sample(seq(Sys.Date() - 5, Sys.Date(), by = "1 days"), 100L,
+ TRUE),
+count = sample(10L, 100L, TRUE)
+ )
>
> # coerce tapply's result to class "data.frame"
> res <- with(data, tapply(count, Date, mean)) |> as.data.frame()
Error: unexpected '>' in "res <- with(data, tapply(count, Date, mean)) |>"
> # assign a dates column from the row names
> res$Date <- row.names(res)
Error in row.names(res) : object 'res' not found
> # cosmetics
> names(res)[2:1] <- names(data)
Error in names(res)[2:1] <- names(data) : object 'res' not found
> # note that the row names are still tapply's names vector
> # and that the columns order is not Date/count. Both are fixed
> # after the calculations.
> res

You can see that the error message is on the pipe. Please, let me know
where I am missing it.
Thanks.

On Wed, Mar 27, 2024 at 10:45 PM Rui Barradas  wrote:

> Às 08:58 de 27/03/2024, Ogbos Okike escreveu:
> > Dear Rui,
> > Nice to hear from you!
> >
> > I am sorry for the omission and I have taken note.
> >
> > Many thanks for responding. The second solution looks elegant as it
> quickly
> > resolved the problem.
> >
> > Please, take a second look at the first solution. It refused to run.
> Looks
> > as if the pipe is not properly positioned. Efforts to correct it and get
> it
> > run failed. If you can look further, it would be great. If time does not
> > permit, I am fine too.
> >
> > But having the too solutions will certainly make the subject more
> > interesting.
> > Thank you so much.
> > With warmest regards from
> > Ogbos
> >
> > On Wed, Mar 27, 2024 at 8:44 AM Rui Barradas 
> wrote:
> >
> >> Às 04:30 de 27/03/2024, Ogbos Okike escreveu:
> >>> Warm greetings to you all.
> >>>
> >>> Using the tapply function below:
> >>> data<-read.table("FD1month",col.names = c("Dates","count"))
> >>> x=data$count
> >>>f<-factor(data$Dates)
> >>> AB<- tapply(x,f,mean)
> >>>
> >>>
> >>> I made a simple calculation. The result, stored in AB, is of the form
> >>> below. But an effort to write AB to a file as a data frame fails. When
> I
> >>> use the write table, it only produces the count column and strip of the
> >>> first column (date).
> >>>
> >>> 2005-11-01 2005-12-01 2006-01-01 2006-02-01 2006-03-01 2006-04-01
> >>> 2006-05-01
> >>>-4.106887  -4.259154  -5.836090  -4.756757  -4.118011  -4.487942
> >>>-4.430705
> >>> 2006-06-01 2006-07-01 2006-08-01 2006-09-01 2006-10-01 2006-11-01
> >>> 2006-12-01
> >>>-3.856727  -6.067103  -6.418767  -4.383031  -3.985805  -4.768196
> >>> -10.072579
> >>> 2007-01-01 2007-02-01 2007-03-01 2007-04-01 2007-05-01 2007-06-01
> >>> 2007-07-01
> >>>-5.342338  -4.653128  -4.325094  -4.525373  -4.574783  -3.915600
> >>>-4.127980
> >>> 2007-08-01 2007-09-01 2007-10-01 2007-11-01 2007-12-01 2008-01-01
> >>> 2008-02-01
> >>>-3.952150  -4.033518  -4.532878  -4.522941  -4.485693  -3.922155
> >>>-4.183578
> >>> 2008-03-01 2008-04-01 2008-05-01 2008-06-01 2008-07-01 2008-08-01
> >>> 2008-09-01
> >>>-4.336969  -3.813306  -4.296579  -4.575095  -4.036036  -4.727994
> >>>-4.347428
> >>> 2008-10-01 2008-11-01 2008-12-01
> >>>-4.029918  -4.260326  -4.454224
> >>>
> >>> But the normal format I wish to display only appears on the terminal,
> >>> leading me to copy it and paste into a text file. That is, when I enter
> >> AB
> >>> on the terminal, it returns a format in the form:
> >>>
> >>> 008-02-01  -4.183578
> >>> 2008-03-01  -4.336969
> >>> 2008-04-01  -3.813306
> >>> 2008-05-01  -4.296579
> >>> 2008-06-01  -4.575095
> >>> 2008-07-01  -4.036036
> >>> 2008-08-01  -4.727994
> >>> 2008-09-01  -4.347428
> >>> 2008-10-01  -4.029918
> >>> 2008-11-01  -4.260326
> >>> 2008-12-01  -4.454224
> >>>
> >>> Now, my question: How do I write out two columns displayed by AB on the
> >>> terminal to a file?
> >>>
> >>> I have tried using AB<-data.frame(AB) but it doesn't work either.
> >>>
> >>> Many thanks for your time.
> >>> Ogbos
> >>>
> >>>[[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.
> >> Hello,
> >>
> >> The main trick is to pipe to as.data.frame. But the result will have one
> >> column only, you must assign the dates from the df's row names.
> >> I also include an aggregate solution.
> >>
> >>
> >>
> >> # create a test data set
> >> set.seed(2024)
> >> data <- data.frame(
> >> Date = sample(seq(Sys.Date() - 5, Sys.Date(), by = "1 days"), 100L,
> >> TRUE),
> >> count = sample(10L, 100L, TRUE)
> 

Re: [R] Output of tapply function as data frame: Problem Fixed

2024-03-28 Thread Jeff Newmiller via R-help
I would guess your version of R is earlier than 4.1, when the built-in pipe was 
introduced to the language

On March 28, 2024 6:43:05 PM PDT, Ogbos Okike  wrote:
>Dear Rui,
>Thanks again for resolving this. I have already started using the version
>that works for me.
>
>But to clarify the second part, please let me paste the what I did and the
>error message:
>
>> set.seed(2024)
>> data <- data.frame(
>+Date = sample(seq(Sys.Date() - 5, Sys.Date(), by = "1 days"), 100L,
>+ TRUE),
>+count = sample(10L, 100L, TRUE)
>+ )
>>
>> # coerce tapply's result to class "data.frame"
>> res <- with(data, tapply(count, Date, mean)) |> as.data.frame()
>Error: unexpected '>' in "res <- with(data, tapply(count, Date, mean)) |>"
>> # assign a dates column from the row names
>> res$Date <- row.names(res)
>Error in row.names(res) : object 'res' not found
>> # cosmetics
>> names(res)[2:1] <- names(data)
>Error in names(res)[2:1] <- names(data) : object 'res' not found
>> # note that the row names are still tapply's names vector
>> # and that the columns order is not Date/count. Both are fixed
>> # after the calculations.
>> res
>
>You can see that the error message is on the pipe. Please, let me know
>where I am missing it.
>Thanks.
>
>On Wed, Mar 27, 2024 at 10:45 PM Rui Barradas  wrote:
>
>> Às 08:58 de 27/03/2024, Ogbos Okike escreveu:
>> > Dear Rui,
>> > Nice to hear from you!
>> >
>> > I am sorry for the omission and I have taken note.
>> >
>> > Many thanks for responding. The second solution looks elegant as it
>> quickly
>> > resolved the problem.
>> >
>> > Please, take a second look at the first solution. It refused to run.
>> Looks
>> > as if the pipe is not properly positioned. Efforts to correct it and get
>> it
>> > run failed. If you can look further, it would be great. If time does not
>> > permit, I am fine too.
>> >
>> > But having the too solutions will certainly make the subject more
>> > interesting.
>> > Thank you so much.
>> > With warmest regards from
>> > Ogbos
>> >
>> > On Wed, Mar 27, 2024 at 8:44 AM Rui Barradas 
>> wrote:
>> >
>> >> Às 04:30 de 27/03/2024, Ogbos Okike escreveu:
>> >>> Warm greetings to you all.
>> >>>
>> >>> Using the tapply function below:
>> >>> data<-read.table("FD1month",col.names = c("Dates","count"))
>> >>> x=data$count
>> >>>f<-factor(data$Dates)
>> >>> AB<- tapply(x,f,mean)
>> >>>
>> >>>
>> >>> I made a simple calculation. The result, stored in AB, is of the form
>> >>> below. But an effort to write AB to a file as a data frame fails. When
>> I
>> >>> use the write table, it only produces the count column and strip of the
>> >>> first column (date).
>> >>>
>> >>> 2005-11-01 2005-12-01 2006-01-01 2006-02-01 2006-03-01 2006-04-01
>> >>> 2006-05-01
>> >>>-4.106887  -4.259154  -5.836090  -4.756757  -4.118011  -4.487942
>> >>>-4.430705
>> >>> 2006-06-01 2006-07-01 2006-08-01 2006-09-01 2006-10-01 2006-11-01
>> >>> 2006-12-01
>> >>>-3.856727  -6.067103  -6.418767  -4.383031  -3.985805  -4.768196
>> >>> -10.072579
>> >>> 2007-01-01 2007-02-01 2007-03-01 2007-04-01 2007-05-01 2007-06-01
>> >>> 2007-07-01
>> >>>-5.342338  -4.653128  -4.325094  -4.525373  -4.574783  -3.915600
>> >>>-4.127980
>> >>> 2007-08-01 2007-09-01 2007-10-01 2007-11-01 2007-12-01 2008-01-01
>> >>> 2008-02-01
>> >>>-3.952150  -4.033518  -4.532878  -4.522941  -4.485693  -3.922155
>> >>>-4.183578
>> >>> 2008-03-01 2008-04-01 2008-05-01 2008-06-01 2008-07-01 2008-08-01
>> >>> 2008-09-01
>> >>>-4.336969  -3.813306  -4.296579  -4.575095  -4.036036  -4.727994
>> >>>-4.347428
>> >>> 2008-10-01 2008-11-01 2008-12-01
>> >>>-4.029918  -4.260326  -4.454224
>> >>>
>> >>> But the normal format I wish to display only appears on the terminal,
>> >>> leading me to copy it and paste into a text file. That is, when I enter
>> >> AB
>> >>> on the terminal, it returns a format in the form:
>> >>>
>> >>> 008-02-01  -4.183578
>> >>> 2008-03-01  -4.336969
>> >>> 2008-04-01  -3.813306
>> >>> 2008-05-01  -4.296579
>> >>> 2008-06-01  -4.575095
>> >>> 2008-07-01  -4.036036
>> >>> 2008-08-01  -4.727994
>> >>> 2008-09-01  -4.347428
>> >>> 2008-10-01  -4.029918
>> >>> 2008-11-01  -4.260326
>> >>> 2008-12-01  -4.454224
>> >>>
>> >>> Now, my question: How do I write out two columns displayed by AB on the
>> >>> terminal to a file?
>> >>>
>> >>> I have tried using AB<-data.frame(AB) but it doesn't work either.
>> >>>
>> >>> Many thanks for your time.
>> >>> Ogbos
>> >>>
>> >>>[[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.
>> >> Hello,
>> >>
>> >> The main trick is to pipe to as.data.frame. But the result will have one
>> >> column only, you must assign 

Re: [R] Output of tapply function as data frame: Problem Fixed

2024-03-28 Thread Rui Barradas

Às 01:43 de 29/03/2024, Ogbos Okike escreveu:

Dear Rui,
Thanks again for resolving this. I have already started using the version
that works for me.

But to clarify the second part, please let me paste the what I did and the
error message:


set.seed(2024)
data <- data.frame(

+Date = sample(seq(Sys.Date() - 5, Sys.Date(), by = "1 days"), 100L,
+ TRUE),
+count = sample(10L, 100L, TRUE)
+ )


# coerce tapply's result to class "data.frame"
res <- with(data, tapply(count, Date, mean)) |> as.data.frame()

Error: unexpected '>' in "res <- with(data, tapply(count, Date, mean)) |>"

# assign a dates column from the row names
res$Date <- row.names(res)

Error in row.names(res) : object 'res' not found

# cosmetics
names(res)[2:1] <- names(data)

Error in names(res)[2:1] <- names(data) : object 'res' not found

# note that the row names are still tapply's names vector
# and that the columns order is not Date/count. Both are fixed
# after the calculations.
res


You can see that the error message is on the pipe. Please, let me know
where I am missing it.
Thanks.

On Wed, Mar 27, 2024 at 10:45 PM Rui Barradas  wrote:


Às 08:58 de 27/03/2024, Ogbos Okike escreveu:

Dear Rui,
Nice to hear from you!

I am sorry for the omission and I have taken note.

Many thanks for responding. The second solution looks elegant as it

quickly

resolved the problem.

Please, take a second look at the first solution. It refused to run.

Looks

as if the pipe is not properly positioned. Efforts to correct it and get

it

run failed. If you can look further, it would be great. If time does not
permit, I am fine too.

But having the too solutions will certainly make the subject more
interesting.
Thank you so much.
With warmest regards from
Ogbos

On Wed, Mar 27, 2024 at 8:44 AM Rui Barradas 

wrote:



Às 04:30 de 27/03/2024, Ogbos Okike escreveu:

Warm greetings to you all.

Using the tapply function below:
data<-read.table("FD1month",col.names = c("Dates","count"))
x=data$count
f<-factor(data$Dates)
AB<- tapply(x,f,mean)


I made a simple calculation. The result, stored in AB, is of the form
below. But an effort to write AB to a file as a data frame fails. When

I

use the write table, it only produces the count column and strip of the
first column (date).

2005-11-01 2005-12-01 2006-01-01 2006-02-01 2006-03-01 2006-04-01
2006-05-01
-4.106887  -4.259154  -5.836090  -4.756757  -4.118011  -4.487942
-4.430705
2006-06-01 2006-07-01 2006-08-01 2006-09-01 2006-10-01 2006-11-01
2006-12-01
-3.856727  -6.067103  -6.418767  -4.383031  -3.985805  -4.768196
-10.072579
2007-01-01 2007-02-01 2007-03-01 2007-04-01 2007-05-01 2007-06-01
2007-07-01
-5.342338  -4.653128  -4.325094  -4.525373  -4.574783  -3.915600
-4.127980
2007-08-01 2007-09-01 2007-10-01 2007-11-01 2007-12-01 2008-01-01
2008-02-01
-3.952150  -4.033518  -4.532878  -4.522941  -4.485693  -3.922155
-4.183578
2008-03-01 2008-04-01 2008-05-01 2008-06-01 2008-07-01 2008-08-01
2008-09-01
-4.336969  -3.813306  -4.296579  -4.575095  -4.036036  -4.727994
-4.347428
2008-10-01 2008-11-01 2008-12-01
-4.029918  -4.260326  -4.454224

But the normal format I wish to display only appears on the terminal,
leading me to copy it and paste into a text file. That is, when I enter

AB

on the terminal, it returns a format in the form:

008-02-01  -4.183578
2008-03-01  -4.336969
2008-04-01  -3.813306
2008-05-01  -4.296579
2008-06-01  -4.575095
2008-07-01  -4.036036
2008-08-01  -4.727994
2008-09-01  -4.347428
2008-10-01  -4.029918
2008-11-01  -4.260326
2008-12-01  -4.454224

Now, my question: How do I write out two columns displayed by AB on the
terminal to a file?

I have tried using AB<-data.frame(AB) but it doesn't work either.

Many thanks for your time.
Ogbos

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

Hello,

The main trick is to pipe to as.data.frame. But the result will have one
column only, you must assign the dates from the df's row names.
I also include an aggregate solution.



# create a test data set
set.seed(2024)
data <- data.frame(
 Date = sample(seq(Sys.Date() - 5, Sys.Date(), by = "1 days"), 100L,
TRUE),
 count = sample(10L, 100L, TRUE)
)

# coerce tapply's result to class "data.frame"
res <- with(data, tapply(count, Date, mean)) |> as.data.frame()
# assign a dates column from the row names
res$Date <- row.names(res)
# cosmetics
names(res)[2:1] <- names(data)
# note that the row names are still tapply's names vector
# and that the columns order is not Date/count. Both are fixed
# after the calculations.
res
#>   count   Date
#> 2024-03-22 5.416667 2024-03-22
#> 2024-03-23 5.50 2024-03-23
#> 2024-03-24 6.00