smpa01
March 12, 2019, 1:25pm
1
Hi,
I have a dataframe as following
Start <- c(1, 6,11,15)
End <- c(5,10,11,17)
df <- data.frame(Start,End)
Sample_Data_File
Is it possible to generate a dataframe with a 3rd column that will keep the integers in sequence between Start and End values.
I attached an excel file with input and desired output in dataframe.
Any help is greatly appreciated. I am extremely new to R.
Thank you in advance.
Welcome to R!
Are you looking for something like this?
Start <- c(1, 6, 11, 15)
End <- c(5, 10, 11, 17)
df <- data.frame(Start,
End)
Custom <- c()
for (i in seq_along(along.with = Start))
{
Custom <- c(Custom, Start[i]:End[i])
}
lengths <- (End - Start + 1)
df <- cbind(Start = rep.int(x = Start,
times = lengths),
End = rep.int(x = End,
times = lengths),
Custom)
Created on 2019-03-12 by the reprex package (v0.2.1)
Hope this helps.
smpa01
March 12, 2019, 1:48pm
3
@Yarnabrina thanks for your response.It perfectly works in R and I have my desired output.
However, I was intending to get this output by running the same code in the R console in Power BI. The code fails in Power BI.
Anyway, thanks for the help.
joels
March 12, 2019, 1:57pm
4
Here's an approach using a dplyr
pipe and nesting:
library(tidyverse)
df %>%
# Need to operate by row, so group by row number
group_by(r=row_number()) %>%
# Create nested list column containing the sequence for each pair of Start, End values
mutate(custom = list(Start:End)) %>%
# Remove the row-number column, which is no longer needed
ungroup %>% select(-r) %>%
# Unnest the list column to get the desired "long" data frame
unnest()
Start End custom
<dbl> <dbl> <int>
1 1 5 1
2 1 5 2
3 1 5 3
4 1 5 4
5 1 5 5
6 6 10 6
7 6 10 7
8 6 10 8
9 6 10 9
10 6 10 10
11 11 11 11
12 15 17 15
13 15 17 16
14 15 17 17
Another option is to create a vectorized helper function that inherently operates by row:
fnc = Vectorize(function(x,y) x:y)
df %>%
mutate(custom = fnc(Start, End)) %>%
unnest()
system
Closed
April 2, 2019, 2:01pm
5
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.