How to implement extract/separate functions (from dplyr and tidyr) to separate a column into multiple columns. based on arbitrary values?

r split column into multiple columns by separator
r separate function
r split column into multiple rows
r split dataframe by multiple columns
r split string by delimiter
tidyr separate multiple columns
r split vector into columns
split dataframe string column into multiple columns

I have a column:

Y = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)

I would like to split into multiple columns, based on the positions of the column values. For instance, I would like:

Y1=c(1,2,3,4,5)
Y2=c(6,7,8,9,10)
Y3=c(11,12,13,14,15)
Y4=c(16,17,18,19,20)

Since I am working with a big data time series set, the divisions will be arbitrary depending on the length of one time period.

You can use the base split to split this vector into vectors that are each 5 items long. You could also use a variable to store this interval length.

Using rep with each = 5, and creating a sequence programmatically, gets you a sequence of the numbers 1, 2, ... up to the length divided by 5 (in this case, 4), each 5 times consecutively. Then split returns a list of vectors.

It's worth noting that a variety of SO posts will recommend you store similar data in lists such as this, rather than creating multiple variables, so I'm leaving it in list form here.

Y <- 1:20

breaks <- rep(1:(length(Y) / 5), each = 5)
split(Y, breaks)
#> $`1`
#> [1] 1 2 3 4 5
#> 
#> $`2`
#> [1]  6  7  8  9 10
#> 
#> $`3`
#> [1] 11 12 13 14 15
#> 
#> $`4`
#> [1] 16 17 18 19 20

Created on 2019-02-12 by the reprex package (v0.2.1)

How to implement extract/separate functions (from dplyr and tidyr) to , I have a column: I would like to split into multiple columns, based on the positions of the You can use the base split to split this vector into vectors that are each 5 items long. You could also use a variable to store this interval length. you store similar data in lists such as this, rather than creating multiple� In tidyr: Tidy Messy Data. Description Usage Arguments See Also Examples. View source: R/extract.R. Description. Given a regular expression with capturing groups, extract() turns each group into a new column.

Not a dplyr solution, but I believe the easiest way would involve using matrices.

foo = function(data, sep.in=5) {
data.matrix = matrix(data,ncol=5)
data.df = as.data.frame(data.matrix)
return(data.df)
}

I have not tested it but this function should create a data.frame who can be merge to a existing one using cbind()

Manipulating, analyzing and exporting data with tidyverse, Select certain columns in a data frame with the dplyr function select . Use summarize , group_by , and count to split a data frame into groups of observations, It pairs nicely with tidyr which enables you to swiftly convert between different data The results from a base R function sometimes depend on the type of data. extract: Extract a character column into multiple columns using extract_numeric: Extract numeric component of variable. fill: Fill in missing values with previous or next value; fish_encounters: Fish encounters; full_seq: Create the full sequence of values in a vector; gather: Gather columns into key-value pairs

We can make use of split (writing the commented code as solution) to split the vector into a list of vectors.

lst <- split(Y, as.integer(gl(length(Y), 5, length(Y))))
lst
#$`1`
#[1] 1 2 3 4 5

#$`2`
#[1]  6  7  8  9 10

#$`3`
#[1] 11 12 13 14 15

#$`4`
#[1] 16 17 18 19 20

Here, the gl create a grouping index by specifying the n, k and length parameters where n - an integer giving the number of levels, k - an integer giving the number of replications, and length -an integer giving the length of the result.

In our case, we want to have 'k' as 5.

as.integer(gl(length(Y), 5, length(Y)))
#[1] 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4

If we want to have multiple objects in the global environment, use list2env

list2env(setNames(lst, paste0("Y", seq_along(lst))), envir = .GlobalEnv)
Y1
#[1] 1 2 3 4 5
Y2
#[1]  6  7  8  9 10
Y3
#[1] 11 12 13 14 15
Y4
#[1] 16 17 18 19 20

Or as the OP mentioned dplyr/tidyr in the question, we can use those packages as well

library(tidyverse)
tibble(Y) %>%
   group_by(grp = (row_number()-1) %/% 5 + 1) %>% 
   summarise(Y = list(Y)) %>%
   pull(Y)
#[[1]]
#[1] 1 2 3 4 5

#[[2]]
#[1]  6  7  8  9 10

#[[3]]
#[1] 11 12 13 14 15

#[[4]]
#[1] 16 17 18 19 20
data
Y <- 1:20

separate: Separate a character column into multiple columns with a , separate: Separate a character column into multiple columns with a In tidyr: Tidy Messy Data. Description Names of new variables to create as character vector. The default value is a regular expression that matches any sequence of unite() , the complement, extract() which uses regular expression capturing groups. As you can see based on the output of the RStudio console, we created a new data frame consisting of two columns X1 and X2. The first column contains the values before the separator “-” and the second column contains the values at the right side of the delimiter “-“.

Split Data Frame Variable into Multiple Columns in R (3 Examples), How to separate a character string column into multiple variables - 3 R If we want to split our variable with Base R, we can use a combination of the The output of the function is a matrix consisting of two columns with the same values as shown Another popular alternative for splitting data is based on the tidyr package. Add new columns to a data frame that are functions of existing columns with mutate. Use the split-apply-combine concept for data analysis. Use summarize , group_by , and count to split a data frame into groups of observations, apply summary statistics for each group, and then combine the results.

Data Tidying � Data Science with R, In the wild, data sets come in many different formats, but each computer You can also use R to report your results when you are finished, and you can values in your data set with the the spread() and gather() functions of the tidyr package. In tidy data, each variable is assigned to its own column, i.e., its own vector in� Can I use separate() or extract() from tidyr to split a numeric value of variable length into its component digits? 0 R: Separating out a mixed data column, date above multiple times

12 Tidy data, Following three rules makes a dataset tidy: variables are in columns, in mutate and summary functions, most built-in R functions work with vectors of values. we need to use dplyr::left_join() , which you'll learn about in relational data. separate() pulls apart one column into multiple columns, by splitting wherever a� This tutorial provides you with the basic understanding of the four fundamental functions of data tidying that tidyr provides: gather() makes “wide” data longer; spread() makes “long” data wider; separate() splits a single column into multiple columns; unite() combines multiple columns into a single column; Additional Resources

Comments
  • It doesn't look like R syntax. If Y <- 1:20; split(Y, as.integer(gl(length(Y), 5, length(Y))))
  • Made the modifications!
  • with tidyverse tibble(Y) %>% group_by(grp = (row_number()-1) %/% 5 + 1) %>% summarise(Y = list(Y))
  • stackoverflow.com/questions/3302356/how-to-split-a-data-frame
  • Could you please include some code, on how to get the same in multiple variables. I intend to create a moving window, to visualize multiple plots of these divisions.
  • If you need help with a wider scope of the problem, you should update the question to include more data or more situations that what you initially described