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

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

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])

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

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

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

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

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

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

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 =