0

I have an integer as a column that I would like to split into multiple, seperate integers

Creating a list of dataframes using split() doesn't work for my later purposes

df <- as.data.frame(runif(n = 10000, min = 1, max = 10))

where split() creates a list of dataframe which I can't use for further purposes, where I need a separate integer as "Values"

map.split <- split(df, (as.numeric(rownames(df)) - 1) %/% 250) # this is not the trick

My goal is to split the column into different integer (not saved under the Global Environment "Data", but "Values")

This would be the slow way:

VecList1 <- df[1:250,]
VecList2 <- df[251:500,]

with

str(VecList1)
Int [1:250] 1 1 10 5 3 ....

Any advice welcome

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • 2
    Can you give an example of what your desired output would be? – iod Oct 24 '19 at 15:41
  • A set of integers as shown in the lower part of my question, each 250 values – Thijs van den Burg Oct 24 '19 at 16:14
  • Your output (integers) is inconsistent with your sample input (floats, made with `runif`). Really, please make this question *reproducible* with **small** consistent sample data and expected output. Refs: https://stackoverflow.com/questions/5963269, https://stackoverflow.com/help/mcve, and https://stackoverflow.com/tags/r/info. – r2evans Oct 24 '19 at 16:33
  • Your use of columns and integers is confusing. You want a series of vectors? – iod Oct 24 '19 at 17:09

2 Answers2

0

If I'm interpreting correctly (not clear to me), here's a reduced problem and what I think you're asking for.

set.seed(2)
df <- data.frame(x = runif(10, min = 1, max = 10))
df$Values <- (seq_len(nrow(df))-1) %/% 4
df
#           x Values
# 1  2.663940      0
# 2  7.321366      0
# 3  6.159937      0
# 4  2.512467      0
# 5  9.494554      1
# 6  9.491275      1
# 7  2.162431      1
# 8  8.501039      1
# 9  5.212167      2
# 10 5.949854      2

If all you need is that Values column as its own object, then you can just change df$Values <- ... to Values <- ....

r2evans
  • 141,215
  • 6
  • 77
  • 149
0

Here's one way of doing this (although it's probably better to figure out a way where you don't need a series of separate vectors, but rather work with columns in a single matrix):

df <- data.frame(a=runif(n = 10000, min = 1, max = 10))
mx<-matrix(df$a,nrow=250)
for (i in 1:NCOL(mx)) {
  assign(paste0("VecList",i),mx[,i])}

Note: using assign is generally not advisable. Whatever it is you're trying to achieve, there's probably a better way of doing it without creating a series of new vectors in the global environment.

iod
  • 7,412
  • 2
  • 17
  • 36