How to modify data frame stored in a list

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

How to modify data frame stored in a list

Honkit Wong
Dear R community,
I have a question seems very simple but have trouble to do it.
I have a list which stores many data frames. Now, I want to perform log10 on one column in each data frame in the list and save the value as a new column back to the original data frame in the list. How do I quickly do that with lapply function ?

Many thanks.
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to modify data frame stored in a list

Jeff Newmiller
Do you want it to run quickly or be quick to write?

Why have you specified that you want a solution that uses lapply? (Such constraints often arise in the context of homework, whereas someone interested in getting the job done does not usually care about which function is used.)

On June 18, 2018 1:46:57 PM PDT, Honkit Wong <[hidden email]> wrote:

>Dear R community,
>I have a question seems very simple but have trouble to do it.
>I have a list which stores many data frames. Now, I want to perform
>log10 on one column in each data frame in the list and save the value
>as a new column back to the original data frame in the list. How do I
>quickly do that with lapply function ?
>
>Many thanks.
>______________________________________________
>[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to modify data frame stored in a list

Bert Gunter-2
In reply to this post by Honkit Wong
It depends on whether you wish to refer to the column to be logged by name
or index.

Reprex:

set.seed(1234)
dat <- lapply(1:3, function(i)data.frame(a = runif(5), b =
sample(letters,5)))

## by numerical index of column
d <- lapply(dat,function(x){x[,"logged"]<- log10(x[,1]); x})

## by name of column
dd <- lapply(dat,function(x){x[,"logged"]<- log10(x[,"a"]); x})

There are also slight variations on how you can do the "[" indexing that
others may post.
Note that you have to return the modified data frame in the function.

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 Mon, Jun 18, 2018 at 1:46 PM, Honkit Wong <[hidden email]> wrote:

> Dear R community,
> I have a question seems very simple but have trouble to do it.
> I have a list which stores many data frames. Now, I want to perform log10
> on one column in each data frame in the list and save the value as a new
> column back to the original data frame in the list. How do I quickly do
> that with lapply function ?
>
> Many thanks.
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to modify data frame stored in a list

Honkit Wong
Thanks!
Why have to add “x” at the end of function, which was what I missed.

> On Jun 18, 2018, at 2:53 PM, Bert Gunter <[hidden email]> wrote:
>
> It depends on whether you wish to refer to the column to be logged by name or index.
>
> Reprex:
>
> set.seed(1234)
> dat <- lapply(1:3, function(i)data.frame(a = runif(5), b = sample(letters,5)))
>
> ## by numerical index of column
> d <- lapply(dat,function(x){x[,"logged"]<- log10(x[,1]); x})
>
> ## by name of column
> dd <- lapply(dat,function(x){x[,"logged"]<- log10(x[,"a"]); x})
>
> There are also slight variations on how you can do the "[" indexing that others may post.
> Note that you have to return the modified data frame in the function.
>
> 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 Mon, Jun 18, 2018 at 1:46 PM, Honkit Wong <[hidden email]> wrote:
>> Dear R community,
>> I have a question seems very simple but have trouble to do it.
>> I have a list which stores many data frames. Now, I want to perform log10 on one column in each data frame in the list and save the value as a new column back to the original data frame in the list. How do I quickly do that with lapply function ?
>>
>> Many thanks.
>> ______________________________________________
>> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to modify data frame stored in a list

Bert Gunter-2
search:

"Default function return R"

-- 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 Mon, Jun 18, 2018 at 2:58 PM, Honkit Wong <[hidden email]> wrote:

> Thanks!
> Why have to add “x” at the end of function, which was what I missed.
>
> On Jun 18, 2018, at 2:53 PM, Bert Gunter <[hidden email]> wrote:
>
> It depends on whether you wish to refer to the column to be logged by name
> or index.
>
> Reprex:
>
> set.seed(1234)
> dat <- lapply(1:3, function(i)data.frame(a = runif(5), b =
> sample(letters,5)))
>
> ## by numerical index of column
> d <- lapply(dat,function(x){x[,"logged"]<- log10(x[,1]); x})
>
> ## by name of column
> dd <- lapply(dat,function(x){x[,"logged"]<- log10(x[,"a"]); x})
>
> There are also slight variations on how you can do the "[" indexing that
> others may post.
> Note that you have to return the modified data frame in the function.
>
> 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 Mon, Jun 18, 2018 at 1:46 PM, Honkit Wong <[hidden email]> wrote:
>
>> Dear R community,
>> I have a question seems very simple but have trouble to do it.
>> I have a list which stores many data frames. Now, I want to perform log10
>> on one column in each data frame in the list and save the value as a new
>> column back to the original data frame in the list. How do I quickly do
>> that with lapply function ?
>>
>> Many thanks.
>> ______________________________________________
>> [hidden email] 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/posti
>> ng-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to modify data frame stored in a list

R help mailing list-2
In reply to this post by Honkit Wong
 >Thanks!
>Why have to add “x” at the end of function, which was what I missed.

You can see for yourself with some tests:

f1 <- function(x) { x[1] <- 10 }
f2 <- function(x) { x[1] <- 10 ; x }

print(f1(1:3)) # 10
print(f2(1:3)) # 10 2 3

An assignment returns the value of its right hand side but you want to
return the altered input.

Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Mon, Jun 18, 2018 at 2:58 PM, Honkit Wong <[hidden email]> wrote:

> Thanks!
> Why have to add “x” at the end of function, which was what I missed.
>
> > On Jun 18, 2018, at 2:53 PM, Bert Gunter <[hidden email]> wrote:
> >
> > It depends on whether you wish to refer to the column to be logged by
> name or index.
> >
> > Reprex:
> >
> > set.seed(1234)
> > dat <- lapply(1:3, function(i)data.frame(a = runif(5), b =
> sample(letters,5)))
> >
> > ## by numerical index of column
> > d <- lapply(dat,function(x){x[,"logged"]<- log10(x[,1]); x})
> >
> > ## by name of column
> > dd <- lapply(dat,function(x){x[,"logged"]<- log10(x[,"a"]); x})
> >
> > There are also slight variations on how you can do the "[" indexing that
> others may post.
> > Note that you have to return the modified data frame in the function.
> >
> > 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 Mon, Jun 18, 2018 at 1:46 PM, Honkit Wong <[hidden email]>
> wrote:
> >> Dear R community,
> >> I have a question seems very simple but have trouble to do it.
> >> I have a list which stores many data frames. Now, I want to perform
> log10 on one column in each data frame in the list and save the value as a
> new column back to the original data frame in the list. How do I quickly do
> that with lapply function ?
> >>
> >> Many thanks.
> >> ______________________________________________
> >> [hidden email] 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]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.