Groupby timestamp data in r The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceHow to join (merge) data frames (inner, outer, left, right)Sum rows in data.frame or matrixCount number of rows within each groupCreate an empty data.frameAdding a new column to each element in a list of tables or data framesdata.table vs dplyr: can one do something well the other can't or does poorly?reshape dataframe based on string valuesGathering data using R - multiple urlsR aggregate on/off timeLarge data set cleaning: How to fill in missing data based on multiple categories and searching by row order
How to support a colleague who finds meetings extremely tiring?
Visa regaring travelling European country
How do you keep chess fun when your opponent constantly beats you?
Do working physicists consider Newtonian mechanics to be "falsified"?
My body leaves; my core can stay
Can we generate random numbers using irrational numbers like π and e?
Match Roman Numerals
How can a C program poll for user input while simultaneously performing other actions in a Linux environment?
First use of “packing” as in carrying a gun
how can a perfect fourth interval be considered either consonant or dissonant?
The following signatures were invalid: EXPKEYSIG 1397BC53640DB551
Working through the single responsibility principle (SRP) in Python when calls are expensive
Windows 10: How to Lock (not sleep) laptop on lid close?
Why are there uneven bright areas in this photo of black hole?
Make it rain characters
Why don't hard Brexiteers insist on a hard border to prevent illegal immigration after Brexit?
Inequalities involving [.]
Sort list of array linked objects by keys and values
"... to apply for a visa" or "... and applied for a visa"?
Is it ok to offer lower paid work as a trial period before negotiating for a full-time job?
What aspect of planet Earth must be changed to prevent the industrial revolution?
How to politely respond to generic emails requesting a PhD/job in my lab? Without wasting too much time
How did the audience guess the pentatonic scale in Bobby McFerrin's presentation?
Python - Fishing Simulator
Groupby timestamp data in r
The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experienceHow to join (merge) data frames (inner, outer, left, right)Sum rows in data.frame or matrixCount number of rows within each groupCreate an empty data.frameAdding a new column to each element in a list of tables or data framesdata.table vs dplyr: can one do something well the other can't or does poorly?reshape dataframe based on string valuesGathering data using R - multiple urlsR aggregate on/off timeLarge data set cleaning: How to fill in missing data based on multiple categories and searching by row order
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am new to R and need help in the following.
I have this data:
Time Temperature
4.460672 96.32332
15.542545 96.32332
16.750386 96.32332
38.709795 96.32332
39.952442 96.32332
41.320898 96.32332
55.375259 96.32332
61.855321 100.47791
66.464590 100.47791
66.966965 100.47791
76.100513 100.47791
76.617365 100.47791
77.186545 100.47791
83.032157 100.47791
87.772441 100.47791
91.786988 100.47791
98.161933 100.47791
101.258411 100.47791
105.186097 100.47791
105.928643 100.47791
111.476967 100.47791
119.426046 100.47791
124.406232 92.70218
135.352858 92.70218
136.545958 92.70218
I need to create a dataframe ‘minute_data’. Each row corresponds to one minute of observations, it contains count of events during that minute and the temperature.
Some minutes may not contain any events. Rows corresponding to such minutes should be excluded from the dataframe.
The dataframe should look like (first 2 rows):
minute count temperature
1 7 96.32332
2 15 100.47791
r
add a comment |
I am new to R and need help in the following.
I have this data:
Time Temperature
4.460672 96.32332
15.542545 96.32332
16.750386 96.32332
38.709795 96.32332
39.952442 96.32332
41.320898 96.32332
55.375259 96.32332
61.855321 100.47791
66.464590 100.47791
66.966965 100.47791
76.100513 100.47791
76.617365 100.47791
77.186545 100.47791
83.032157 100.47791
87.772441 100.47791
91.786988 100.47791
98.161933 100.47791
101.258411 100.47791
105.186097 100.47791
105.928643 100.47791
111.476967 100.47791
119.426046 100.47791
124.406232 92.70218
135.352858 92.70218
136.545958 92.70218
I need to create a dataframe ‘minute_data’. Each row corresponds to one minute of observations, it contains count of events during that minute and the temperature.
Some minutes may not contain any events. Rows corresponding to such minutes should be excluded from the dataframe.
The dataframe should look like (first 2 rows):
minute count temperature
1 7 96.32332
2 15 100.47791
r
add a comment |
I am new to R and need help in the following.
I have this data:
Time Temperature
4.460672 96.32332
15.542545 96.32332
16.750386 96.32332
38.709795 96.32332
39.952442 96.32332
41.320898 96.32332
55.375259 96.32332
61.855321 100.47791
66.464590 100.47791
66.966965 100.47791
76.100513 100.47791
76.617365 100.47791
77.186545 100.47791
83.032157 100.47791
87.772441 100.47791
91.786988 100.47791
98.161933 100.47791
101.258411 100.47791
105.186097 100.47791
105.928643 100.47791
111.476967 100.47791
119.426046 100.47791
124.406232 92.70218
135.352858 92.70218
136.545958 92.70218
I need to create a dataframe ‘minute_data’. Each row corresponds to one minute of observations, it contains count of events during that minute and the temperature.
Some minutes may not contain any events. Rows corresponding to such minutes should be excluded from the dataframe.
The dataframe should look like (first 2 rows):
minute count temperature
1 7 96.32332
2 15 100.47791
r
I am new to R and need help in the following.
I have this data:
Time Temperature
4.460672 96.32332
15.542545 96.32332
16.750386 96.32332
38.709795 96.32332
39.952442 96.32332
41.320898 96.32332
55.375259 96.32332
61.855321 100.47791
66.464590 100.47791
66.966965 100.47791
76.100513 100.47791
76.617365 100.47791
77.186545 100.47791
83.032157 100.47791
87.772441 100.47791
91.786988 100.47791
98.161933 100.47791
101.258411 100.47791
105.186097 100.47791
105.928643 100.47791
111.476967 100.47791
119.426046 100.47791
124.406232 92.70218
135.352858 92.70218
136.545958 92.70218
I need to create a dataframe ‘minute_data’. Each row corresponds to one minute of observations, it contains count of events during that minute and the temperature.
Some minutes may not contain any events. Rows corresponding to such minutes should be excluded from the dataframe.
The dataframe should look like (first 2 rows):
minute count temperature
1 7 96.32332
2 15 100.47791
r
r
asked Mar 22 at 6:05
RushRush
1
1
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can use dplyr
for this:
library(dplyr)
df %>%
mutate(minute = Time %/% 60 + 1) %>%
group_by(minute) %>%
summarise(count = n(),
temperature = first(temperature)) %>%
select(-Time)
Note: Change first
to mean
if you want mean
Thanks Sonny. I tried your code. The data I provided as input in my question is only the 25 rows of a csv file of 3414 rows. I understood your code. My question is where should I mention the csv file name in the code. I ran your code and got this error. Error: object 'Time' not found
– Rush
Mar 22 at 6:48
Then I tweaked your code slightly : df %>% mutate(minute = Course.Project.Data$Time %/% 60 + 1) %>% group_by(minute) %>% summarise(count = n(), temperature = first(temperature)) %>% select(-Time) Got this error: Error: Columnminute
must be length 1000 (the number of rows) or one, not 3414
– Rush
Mar 22 at 6:49
You need to read the csv file intodf
.
– Sonny
Mar 22 at 6:53
df <- read_csv("your_csv_file")
– Sonny
Mar 22 at 7:02
Thank you. Appreciate
– Rush
Mar 22 at 7:05
add a comment |
And (of course) data.table
can also perform this operation
sample data
library( data.table )
DT <- fread("Time Temperature
4.460672 96.32332
15.542545 96.32332
16.750386 96.32332
38.709795 96.32332
39.952442 96.32332
41.320898 96.32332
55.375259 96.32332
61.855321 100.47791
66.464590 100.47791
66.966965 100.47791
76.100513 100.47791
76.617365 100.47791
77.186545 100.47791
83.032157 100.47791
87.772441 100.47791
91.786988 100.47791
98.161933 100.47791
101.258411 100.47791
105.186097 100.47791
105.928643 100.47791
111.476967 100.47791
119.426046 100.47791
124.406232 92.70218
135.352858 92.70218
136.545958 92.70218")
code
It is actually a one-liner, but for readability I added some linebreaks.
DT[, list( count = .N,
temperature = mean( Temperature ) ),
by = .( minute = floor( Time / 60 ) + 1 )]
output
# minute count temperature
# 1: 1 7 96.32332
# 2: 2 15 100.47791
# 3: 3 3 92.70218
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55293781%2fgroupby-timestamp-data-in-r%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use dplyr
for this:
library(dplyr)
df %>%
mutate(minute = Time %/% 60 + 1) %>%
group_by(minute) %>%
summarise(count = n(),
temperature = first(temperature)) %>%
select(-Time)
Note: Change first
to mean
if you want mean
Thanks Sonny. I tried your code. The data I provided as input in my question is only the 25 rows of a csv file of 3414 rows. I understood your code. My question is where should I mention the csv file name in the code. I ran your code and got this error. Error: object 'Time' not found
– Rush
Mar 22 at 6:48
Then I tweaked your code slightly : df %>% mutate(minute = Course.Project.Data$Time %/% 60 + 1) %>% group_by(minute) %>% summarise(count = n(), temperature = first(temperature)) %>% select(-Time) Got this error: Error: Columnminute
must be length 1000 (the number of rows) or one, not 3414
– Rush
Mar 22 at 6:49
You need to read the csv file intodf
.
– Sonny
Mar 22 at 6:53
df <- read_csv("your_csv_file")
– Sonny
Mar 22 at 7:02
Thank you. Appreciate
– Rush
Mar 22 at 7:05
add a comment |
You can use dplyr
for this:
library(dplyr)
df %>%
mutate(minute = Time %/% 60 + 1) %>%
group_by(minute) %>%
summarise(count = n(),
temperature = first(temperature)) %>%
select(-Time)
Note: Change first
to mean
if you want mean
Thanks Sonny. I tried your code. The data I provided as input in my question is only the 25 rows of a csv file of 3414 rows. I understood your code. My question is where should I mention the csv file name in the code. I ran your code and got this error. Error: object 'Time' not found
– Rush
Mar 22 at 6:48
Then I tweaked your code slightly : df %>% mutate(minute = Course.Project.Data$Time %/% 60 + 1) %>% group_by(minute) %>% summarise(count = n(), temperature = first(temperature)) %>% select(-Time) Got this error: Error: Columnminute
must be length 1000 (the number of rows) or one, not 3414
– Rush
Mar 22 at 6:49
You need to read the csv file intodf
.
– Sonny
Mar 22 at 6:53
df <- read_csv("your_csv_file")
– Sonny
Mar 22 at 7:02
Thank you. Appreciate
– Rush
Mar 22 at 7:05
add a comment |
You can use dplyr
for this:
library(dplyr)
df %>%
mutate(minute = Time %/% 60 + 1) %>%
group_by(minute) %>%
summarise(count = n(),
temperature = first(temperature)) %>%
select(-Time)
Note: Change first
to mean
if you want mean
You can use dplyr
for this:
library(dplyr)
df %>%
mutate(minute = Time %/% 60 + 1) %>%
group_by(minute) %>%
summarise(count = n(),
temperature = first(temperature)) %>%
select(-Time)
Note: Change first
to mean
if you want mean
answered Mar 22 at 6:13
SonnySonny
2,1011516
2,1011516
Thanks Sonny. I tried your code. The data I provided as input in my question is only the 25 rows of a csv file of 3414 rows. I understood your code. My question is where should I mention the csv file name in the code. I ran your code and got this error. Error: object 'Time' not found
– Rush
Mar 22 at 6:48
Then I tweaked your code slightly : df %>% mutate(minute = Course.Project.Data$Time %/% 60 + 1) %>% group_by(minute) %>% summarise(count = n(), temperature = first(temperature)) %>% select(-Time) Got this error: Error: Columnminute
must be length 1000 (the number of rows) or one, not 3414
– Rush
Mar 22 at 6:49
You need to read the csv file intodf
.
– Sonny
Mar 22 at 6:53
df <- read_csv("your_csv_file")
– Sonny
Mar 22 at 7:02
Thank you. Appreciate
– Rush
Mar 22 at 7:05
add a comment |
Thanks Sonny. I tried your code. The data I provided as input in my question is only the 25 rows of a csv file of 3414 rows. I understood your code. My question is where should I mention the csv file name in the code. I ran your code and got this error. Error: object 'Time' not found
– Rush
Mar 22 at 6:48
Then I tweaked your code slightly : df %>% mutate(minute = Course.Project.Data$Time %/% 60 + 1) %>% group_by(minute) %>% summarise(count = n(), temperature = first(temperature)) %>% select(-Time) Got this error: Error: Columnminute
must be length 1000 (the number of rows) or one, not 3414
– Rush
Mar 22 at 6:49
You need to read the csv file intodf
.
– Sonny
Mar 22 at 6:53
df <- read_csv("your_csv_file")
– Sonny
Mar 22 at 7:02
Thank you. Appreciate
– Rush
Mar 22 at 7:05
Thanks Sonny. I tried your code. The data I provided as input in my question is only the 25 rows of a csv file of 3414 rows. I understood your code. My question is where should I mention the csv file name in the code. I ran your code and got this error. Error: object 'Time' not found
– Rush
Mar 22 at 6:48
Thanks Sonny. I tried your code. The data I provided as input in my question is only the 25 rows of a csv file of 3414 rows. I understood your code. My question is where should I mention the csv file name in the code. I ran your code and got this error. Error: object 'Time' not found
– Rush
Mar 22 at 6:48
Then I tweaked your code slightly : df %>% mutate(minute = Course.Project.Data$Time %/% 60 + 1) %>% group_by(minute) %>% summarise(count = n(), temperature = first(temperature)) %>% select(-Time) Got this error: Error: Column
minute
must be length 1000 (the number of rows) or one, not 3414– Rush
Mar 22 at 6:49
Then I tweaked your code slightly : df %>% mutate(minute = Course.Project.Data$Time %/% 60 + 1) %>% group_by(minute) %>% summarise(count = n(), temperature = first(temperature)) %>% select(-Time) Got this error: Error: Column
minute
must be length 1000 (the number of rows) or one, not 3414– Rush
Mar 22 at 6:49
You need to read the csv file into
df
.– Sonny
Mar 22 at 6:53
You need to read the csv file into
df
.– Sonny
Mar 22 at 6:53
df <- read_csv("your_csv_file")
– Sonny
Mar 22 at 7:02
df <- read_csv("your_csv_file")
– Sonny
Mar 22 at 7:02
Thank you. Appreciate
– Rush
Mar 22 at 7:05
Thank you. Appreciate
– Rush
Mar 22 at 7:05
add a comment |
And (of course) data.table
can also perform this operation
sample data
library( data.table )
DT <- fread("Time Temperature
4.460672 96.32332
15.542545 96.32332
16.750386 96.32332
38.709795 96.32332
39.952442 96.32332
41.320898 96.32332
55.375259 96.32332
61.855321 100.47791
66.464590 100.47791
66.966965 100.47791
76.100513 100.47791
76.617365 100.47791
77.186545 100.47791
83.032157 100.47791
87.772441 100.47791
91.786988 100.47791
98.161933 100.47791
101.258411 100.47791
105.186097 100.47791
105.928643 100.47791
111.476967 100.47791
119.426046 100.47791
124.406232 92.70218
135.352858 92.70218
136.545958 92.70218")
code
It is actually a one-liner, but for readability I added some linebreaks.
DT[, list( count = .N,
temperature = mean( Temperature ) ),
by = .( minute = floor( Time / 60 ) + 1 )]
output
# minute count temperature
# 1: 1 7 96.32332
# 2: 2 15 100.47791
# 3: 3 3 92.70218
add a comment |
And (of course) data.table
can also perform this operation
sample data
library( data.table )
DT <- fread("Time Temperature
4.460672 96.32332
15.542545 96.32332
16.750386 96.32332
38.709795 96.32332
39.952442 96.32332
41.320898 96.32332
55.375259 96.32332
61.855321 100.47791
66.464590 100.47791
66.966965 100.47791
76.100513 100.47791
76.617365 100.47791
77.186545 100.47791
83.032157 100.47791
87.772441 100.47791
91.786988 100.47791
98.161933 100.47791
101.258411 100.47791
105.186097 100.47791
105.928643 100.47791
111.476967 100.47791
119.426046 100.47791
124.406232 92.70218
135.352858 92.70218
136.545958 92.70218")
code
It is actually a one-liner, but for readability I added some linebreaks.
DT[, list( count = .N,
temperature = mean( Temperature ) ),
by = .( minute = floor( Time / 60 ) + 1 )]
output
# minute count temperature
# 1: 1 7 96.32332
# 2: 2 15 100.47791
# 3: 3 3 92.70218
add a comment |
And (of course) data.table
can also perform this operation
sample data
library( data.table )
DT <- fread("Time Temperature
4.460672 96.32332
15.542545 96.32332
16.750386 96.32332
38.709795 96.32332
39.952442 96.32332
41.320898 96.32332
55.375259 96.32332
61.855321 100.47791
66.464590 100.47791
66.966965 100.47791
76.100513 100.47791
76.617365 100.47791
77.186545 100.47791
83.032157 100.47791
87.772441 100.47791
91.786988 100.47791
98.161933 100.47791
101.258411 100.47791
105.186097 100.47791
105.928643 100.47791
111.476967 100.47791
119.426046 100.47791
124.406232 92.70218
135.352858 92.70218
136.545958 92.70218")
code
It is actually a one-liner, but for readability I added some linebreaks.
DT[, list( count = .N,
temperature = mean( Temperature ) ),
by = .( minute = floor( Time / 60 ) + 1 )]
output
# minute count temperature
# 1: 1 7 96.32332
# 2: 2 15 100.47791
# 3: 3 3 92.70218
And (of course) data.table
can also perform this operation
sample data
library( data.table )
DT <- fread("Time Temperature
4.460672 96.32332
15.542545 96.32332
16.750386 96.32332
38.709795 96.32332
39.952442 96.32332
41.320898 96.32332
55.375259 96.32332
61.855321 100.47791
66.464590 100.47791
66.966965 100.47791
76.100513 100.47791
76.617365 100.47791
77.186545 100.47791
83.032157 100.47791
87.772441 100.47791
91.786988 100.47791
98.161933 100.47791
101.258411 100.47791
105.186097 100.47791
105.928643 100.47791
111.476967 100.47791
119.426046 100.47791
124.406232 92.70218
135.352858 92.70218
136.545958 92.70218")
code
It is actually a one-liner, but for readability I added some linebreaks.
DT[, list( count = .N,
temperature = mean( Temperature ) ),
by = .( minute = floor( Time / 60 ) + 1 )]
output
# minute count temperature
# 1: 1 7 96.32332
# 2: 2 15 100.47791
# 3: 3 3 92.70218
edited Mar 22 at 8:11
answered Mar 22 at 7:25
WimpelWimpel
6,352323
6,352323
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55293781%2fgroupby-timestamp-data-in-r%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown