Get last row of each group in R
You might try:
a %>%
group_by(ID) %>%
arrange(NUM) %>%
slice(n())
R: How to get the last element from each group?
Did you try this?
val_eod <- aggregate(d$val1, by = list(d$day), FUN = tail, n = 1)
R: get last row of each group in dataframe
Package dplyr
has a nice function for doing this.
library(tidyverse)
iris %>%
group_by(Species) %>%
slice_tail(n = 1)
How to select last N observation from each group in dplyr dataframe?
As it is a specific question based on dplyr
1) after the group_by
, use slice
on the row_number()
library(tidyverse)
df %>%
group_by(a) %>%
slice(tail(row_number(), 2))
# A tibble: 8 x 2
# Groups: a [4]
# a b
# <dbl> <dbl>
#1 1 343
#2 1 54
#3 2 55
#4 2 62
#5 3 59
#6 3 -9
#7 4 0
#8 4 -0.5
2) Or use filter
from dplyr
df %>%
group_by(a) %>%
filter(row_number() >= (n() - 1))
3) or with do
and tail
df %>%
group_by(a) %>%
do(tail(., 2))
4) In addition to the tidyverse
, methods, we can also use compact data.table
library(data.table)
setDT(df)[df[, .I[tail(seq_len(.N), 2)], a]$V1]
5) Or by
from base R
by(df, df$a, FUN = tail, 2)
6) or with aggregate
from base R
df[aggregate(c ~ a, transform(df, c = seq_len(nrow(df))), FUN = tail, 2)$c,]
7) or with split
from base R
do.call(rbind, lapply(split(df, df$a), tail, 2))
Get the first to the last element of each group in the form of a datatable using only data.table operations
Select rows until last "B"
value in each group.
library(data.table)
datatable_example[, .SD[seq_len(max(which(b == 'B')))], a]
# a b
#1: 1 A
#2: 1 B
#3: 1 B
#4: 2 B
#5: 2 B
#6: 3 A
#7: 3 B
R get date of first and last element in group
using dplyr
and data.table
:
df %>%
mutate(Day = as.Date(Date)) %>%
group_by(grp = rleid(Day, Element, Event), Day, Element, Event) %>%
summarize(Begin = min(Date),
End = max(Date)) %>%
ungroup() %>%
select(-grp)
Which results in:
# A tibble: 6 x 5
Day Element Event Begin End
<date> <chr> <chr> <chr> <chr>
1 2020-01-01 A OK 2020-01-01 00:00:00 2020-01-01 00:10:00
2 2020-01-01 A Alarm 2020-01-01 00:15:00 2020-01-01 00:25:00
3 2020-01-01 A OK 2020-01-01 00:30:00 2020-01-01 00:30:00
4 2020-01-01 B OK 2020-01-01 00:00:00 2020-01-01 00:05:00
5 2020-01-01 B Alarm 2020-01-01 00:10:00 2020-01-01 00:20:00
6 2020-01-01 B OK 2020-01-01 00:25:00 2020-01-01 00:30:00
Select first and last row from grouped data
There is probably a faster way:
df %>%
group_by(id) %>%
arrange(stopSequence) %>%
filter(row_number()==1 | row_number()==n())
Get the last row of a previous group in data.table
You could do
dt[, newcol := shift(dt[, last(Product), by = Group]$V1)[.GRP], by = Group]
This results in the following updated dt
, where newcol
matches your desired column with the unnecessarily long name. ;)
Product Group LastProductOfPriorGroup newcol
1: A 1 NA NA
2: B 1 NA NA
3: C 2 B B
4: D 2 B B
5: E 2 B B
6: F 3 E E
7: G 3 E E
Let's break the code down from the inside out. I will use ...
to denote the accumulated code:
dt[, last(Product), by = Group]$V1
is getting the last values from each group as a character vector.shift(...)
shifts the character vector in the previous calldt[, newcol := ...[.GRP], by = Group]
groups byGroup
and uses the internal.GRP
values for indexing
Update: Frank brings up a good point about my code above calculating the shift for every group over and over again. To avoid that, we can use either
shifted <- shift(dt[, last(Product), Group]$V1)
dt[, newcol := shifted[.GRP], by = Group]
so that we don't calculate the shift for every group. Or, we can take Frank's nice suggestion in the comments and do the following.
dt[dt[, last(Product), by = Group][, v := shift(V1)], on="Group", newcol := i.v]
Related Topics
Ggplot2: Issues with Dual Y-Axes and Loess Smoothing
Clustering Algorithm for Obtaining Equal Sized Clusters
Reading in Chunks at a Time Using Fread in Package Data.Table
How to Control the Igraph Plot Layout with Fixed Positions
Stl Decomposition of Time Series with Missing Values for Anomaly Detection
How to Delete a Row from a Data.Frame Without Losing the Attributes
R: Insert a Vector as a Row in Data.Frame
R - How to Test for Character(0) in If Statement
Correct Positioning of Multiple Significance Labels on Dodged Groups in Ggplot
How to Make a Post Request with Header and Data Options in R Using Httr::Post
Warning: Non-Integer #Successes in a Binomial Glm! (Survey Packages)
Updating Column in One Dataframe with Value from Another Dataframe Based on Matching Values
Insert Portions of a Markdown Document Inside Another Markdown Document Using Knitr