apply two functions to column

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

apply two functions to column

arnaud Gaboury
I am little lost between all the possibilities to apply a function to
a data.frame or data.table.

Here is mine:

structure(list(name = c("poisonivy", "poisonivy", "poisonivy",
"poisonivy", "poisonivy", "poisonivy", "poisonivy", "poisonivy",
"cruzecontrol", "agreenmamba", "agreenmamba", "vairis", "vairis",
"vairis", "vairis", "vairis", "vairis", "xaeth"), text = c("ok",
"need items ?", "i didn't submit pass codes for a long now",
"ok", "<@U03AEKYL4>: what app are you talking about ?", "some testing
with my irc client",
"ha ha sorry", "for me there is no such question", "Lol.",
"<@U03AEKWTL|agreenmamba> uploaded a file:
<https://enlightened.slack.com/files/agreenmamba/F03KGRF3W/screenshot_2015-02-09-14-31-15.png|regarding:
should I stay or should I go?>",
"<@U032FHV3S> <http://youtu.be/oGIFublvDes>", "ok, see you around",
"yeah, I had a procrastination rush so I started to decode a little",
"<http://ingress.com/intel|ingress.com/intel> when you submit passcodes",
"intel", "what is the cooldown time or how does it work...;",
"anybody knows how does \"Passcode circuitry too hot. Wait for cool
down to enter another passcode.\" works?",
"and people told that agent their geocities experience would never
amount to anything (the convo yesterday) "
), ts = c("1423594336.000138", "1423594311.000136", "1423594294.000135",
"1423594258.000133", "1423594244.000131", "1423497058.000127",
"1423497041.000126", "1423478555.000123", "1423494427.000125",
"1423492370.000124", "1423478364.000121", "1423594358.000139",
"1423594329.000137", "1423594264.000134", "1423594251.000132",
"1423592204.000130", "1423592174.000129", "1423150354.000112"
)), .Names = c("name", "text", "ts"), class = c("data.table",
"data.frame"), row.names = c(NA, -18L))

As you can see, it is of class data.frame and data.table.

I need to work on last column "t"s. These are characters and are times
in epoch format. I want to have time in Posix. I must thus do two
things:
- characters to numeric : myData$ts <- as.numeric(myData$ts)
- epoch to Posix : mapply(as.POSIXct, tz = 'GMT', origin =
'1970-01-01', myMerge$ts)

I wonder:
- if I can apply these two functions at the same time
- what will be the fastest way : use dplyr package, or work with
data.table ? My real data will be much more important than the one
posted.

Thank you for advice and hint.

--

google.com/+arnaudgabourygabx

______________________________________________
[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: apply two functions to column

Jeff Newmiller
You don't need to do these operations in pieces so the mapply is unnecessary. Neither dplyr nor data.table can go faster than (assuming your data frame is called DF):

DF$dtm <- as.POSIXct( as.numeric( DF$ts ), tz="GMT", origin="1970-01-01" )

They can in this case save you from having to retype DF more than once, but in this case even achieving that requires more typing than the above does.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.

On February 12, 2015 6:15:35 AM EST, arnaud gaboury <[hidden email]> wrote:

>I am little lost between all the possibilities to apply a function to
>a data.frame or data.table.
>
>Here is mine:
>
>structure(list(name = c("poisonivy", "poisonivy", "poisonivy",
>"poisonivy", "poisonivy", "poisonivy", "poisonivy", "poisonivy",
>"cruzecontrol", "agreenmamba", "agreenmamba", "vairis", "vairis",
>"vairis", "vairis", "vairis", "vairis", "xaeth"), text = c("ok",
>"need items ?", "i didn't submit pass codes for a long now",
>"ok", "<@U03AEKYL4>: what app are you talking about ?", "some testing
>with my irc client",
>"ha ha sorry", "for me there is no such question", "Lol.",
>"<@U03AEKWTL|agreenmamba> uploaded a file:
><https://enlightened.slack.com/files/agreenmamba/F03KGRF3W/screenshot_2015-02-09-14-31-15.png|regarding:
>should I stay or should I go?>",
>"<@U032FHV3S> <http://youtu.be/oGIFublvDes>", "ok, see you around",
>"yeah, I had a procrastination rush so I started to decode a little",
>"<http://ingress.com/intel|ingress.com/intel> when you submit
>passcodes",
>"intel", "what is the cooldown time or how does it work...;",
>"anybody knows how does \"Passcode circuitry too hot. Wait for cool
>down to enter another passcode.\" works?",
>"and people told that agent their geocities experience would never
>amount to anything (the convo yesterday) "
>), ts = c("1423594336.000138", "1423594311.000136",
>"1423594294.000135",
>"1423594258.000133", "1423594244.000131", "1423497058.000127",
>"1423497041.000126", "1423478555.000123", "1423494427.000125",
>"1423492370.000124", "1423478364.000121", "1423594358.000139",
>"1423594329.000137", "1423594264.000134", "1423594251.000132",
>"1423592204.000130", "1423592174.000129", "1423150354.000112"
>)), .Names = c("name", "text", "ts"), class = c("data.table",
>"data.frame"), row.names = c(NA, -18L))
>
>As you can see, it is of class data.frame and data.table.
>
>I need to work on last column "t"s. These are characters and are times
>in epoch format. I want to have time in Posix. I must thus do two
>things:
>- characters to numeric : myData$ts <- as.numeric(myData$ts)
>- epoch to Posix : mapply(as.POSIXct, tz = 'GMT', origin =
>'1970-01-01', myMerge$ts)
>
>I wonder:
>- if I can apply these two functions at the same time
>- what will be the fastest way : use dplyr package, or work with
>data.table ? My real data will be much more important than the one
>posted.
>
>Thank you for advice and hint.

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