[R] Merging a dataframe after subsetting with respect to several factors
Jeff Newmiller
jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Fri Jun 14 07:55:38 CEST 2019
Sorry... I missed that the mailing list had been removed from this email.
Tina... always keep the mailing list included when you ask questions.
On Thu, 13 Jun 2019, Jeff Newmiller wrote:
> I am sorry I did not read more closely earlier... I agree with Bert... you do
> need to spend some time learning about vectorized operations (one statement
> can affect all of the elements in a column... because a column is a vector).
> However, you have provided an excellent reproducible example so I will give
> you some thoughts on how you should approach studying R.
>
> For instance, you could experiment with the paste0 function:
>
> paste0( df$a, df$b )
>
> and if you want to pass all of the elements of a list to a function as
> arguments...
>
> do.call( paste0, df )
>
> noting that your df data frame is in fact a list of columns.
>
> Of course, that combines all of the columns together, and you only wanted
> some of them, so you really could read the sections on indexing in the
> Introduction to R document that comes with the software and learn that you
> can select some columns by indexing with a vector of integers:
>
> 1:3
>
> df[ , 1:3 ]
>
> then you could put it all together and create a data frame:
>
> df_1b <- data.frame( val = df$time
> , name = do.call( paste0, df[ , 1:3 ] )
> )
>
> Now while I have tried twice to answer your question, I will say that
> responding to Bert by saying "you need it for further calculations" rather
> than describing your larger task is like burning bridges to getting future
> answers. If you come back and ask for help on what to do with your df_1 data
> frame and it could have been achieved more simply without doing any of these
> steps that you asked for, you now risk getting ignored. Perhaps someone else
> will step in at that point, but reducing the pool of experts who are willing
> to respond is not a very wise strategy. But if with every email you learn
> something new about R and how to communicate on a mailing list, perhaps this
> will just be a bump in a long and productive journey.
>
> On Fri, 14 Jun 2019, Tina Chatterjee wrote:
>
>> Dear Bert and Jeff,Sir,
>> I am a beginner in using R. I wanted a data frame because I need it for my
>> further calculations. So, could you please suggest how this can be done
>> using tapply() /filter() function?
>> Regards.
>> Tina
>>
>> On Fri, Jun 14, 2019 at 7:30 AM Bert Gunter <bgunter.4567 using gmail.com> wrote:
>> Jeff:
>> Your solution is not quite what she asked for (she wanted a data
>> frame, not a list).
>>
>> Moreover, most of the time it is done automatically as the first step
>> of a tapply() /filter() type operation or is inherent in modeling and
>> trellis-type plots. I *still* suspect it is unnecessary, but of course
>> I could be wrong. That's why I asked for clarification.
>>
>> Cheers,
>> Bert
>>
>> On Thu, Jun 13, 2019 at 5:34 PM Jeff Newmiller
>> <jdnewmil using dcn.davis.ca.us> wrote:
>> I do it regularly.
>>
>> Base R:
>>
>> result <- split( DF[ , 4, drop=FALSE ], DF[ , -4 ] )
>>
>> Tidyverse:
>>
>> library(tidyr)
>> result <- nest( DF, time )
>> filter( result, "a2"==a & "b1"==b & "c1"==c )[[ "data" ]]
>>
>> On Thu, 13 Jun 2019, Bert Gunter wrote:
>>
>> > Why? I suspect that there is no reason that you need to
>> do this.
>> >
>> > Cheers,
>> > Bert
>> >
>> > Bert Gunter
>> >
>> > "The trouble with having an open mind is that people
>> keep coming along and
>> > sticking things into it."
>> > -- Opus (aka Berkeley Breathed in his "Bloom County"
>> comic strip )
>> >
>> >
>> > On Thu, Jun 13, 2019 at 1:22 PM Tina Chatterjee
>> <tinamunim2019 using gmail.com>
>> > wrote:
>> >
>> >> Hello everyone!
>> >> I have the following dataframe(df).
>> >>
>> >> a<-c("a1","a2","a2","a1","a1","a1")
>> >> b<-c("b1","b1","b1","b1","b1","b2")
>> >> c<-c("c1","c1","c1","c1","c1","c2")
>> >> time <- c(runif(6,0,1))
>> >>
>> >> df<-data.frame(a,b,c,time)
>> >> df
>> >>
>> >> a b c time
>> >> 1 a1 b1 c1 0.28781082
>> >> 2 a2 b1 c1 0.02102591
>> >> 3 a2 b1 c1 0.72479220
>> >> 4 a1 b1 c1 0.41947675
>> >> 5 a1 b1 c1 0.58899855
>> >> 6 a1 b2 c2 0.82414123
>> >>
>> >> Now, I want to extract the time components
>> corresponding
>> >> to the specific combination of the factors. Finally I
>> have made a dataframe
>> >> (df_1) with 2 columns one with the time components and
>> the other with the
>> >> level combinations.
>> >>
>> >> df[df$a=="a1" & df$b=="b1" & df$c=="c1",]$time
>> >> df[df$a=="a2" & df$b=="b1" & df$c=="c1",]$time
>> >> df[df$a=="a1" & df$b=="b2" & df$c=="c2",]$time
>> >>
>> >> val <- c(df[df$a=="a1" & df$b=="b1" &
>> df$c=="c1",]$time,df[df$a=="a2" &
>> >> df$b=="b1" & df$c=="c1",]$time,df[df$a=="a1" &
>> df$b=="b2" &
>> >> df$c=="c2",]$time)
>> >> name <- c(rep("a1b1c1",3),rep("a2b1c1",2),"a1b2c2")
>> >> df_1 <- data.frame(val,name)
>> >>
>> >> I made it manually. In reality I have a lot of
>> treatment combinations. So,
>> >> could you please suggest how can I do this with a loop
>> or any control
>> >> sequence?
>> >> Thanks and regards.
>> >> Tina
>> >>
>> >> [[alternative HTML version deleted]]
>> >>
>> >> ______________________________________________
>> >> 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.
>> >>
>> >
>> > [[alternative HTML version deleted]]
>> >
>> > ______________________________________________
>> > 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.
>> >
>>
>> ---------------------------------------------------------------------------
>>
>> Jeff Newmiller The .....
>> ..... Go Live...
>> DCN:<jdnewmil using dcn.davis.ca.us> Basics: ##.#.
>> ##.#. Live Go...
>> Live: OO#.. Dead:
>> OO#.. Playing
>> Research Engineer (Solar/Batteries O.O#.
>> #.O#. with
>> /Software/Embedded Controllers) .OO#.
>> .OO#. rocks...1k
>> ---------------------------------------------------------------------------
>>
>>
>>
>
> ---------------------------------------------------------------------------
> Jeff Newmiller The ..... ..... Go Live...
> DCN:<jdnewmil using dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
> Live: OO#.. Dead: OO#.. Playing
> Research Engineer (Solar/Batteries O.O#. #.O#. with
> /Software/Embedded Controllers) .OO#. .OO#. rocks...1k
> ---------------------------------------------------------------------------
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil using dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
More information about the R-help
mailing list