2

I want to take a dataset and split it into multiple datasets. For a simplified verson of the problem. Realistically, I will have thousands of rows but I would like to simplify the problem for the purpose of understanding. Suppose you have the following code:

vec = c(1:10)
df = data.frame(vec)
df
   vec
1    1
2    2
3    3
4    4
5    5
6    6
7    7
8    8
9    9
10  10

I would like to split this dataset into rows of 5 observations each and then get the mean for each 5 rows.

So far i've tried to split the code in the following manner:

splitdf = split(df, rep(1:2,each = 5))

Now I would like to get the mean of each group. For example, the mean of the first chunk is 3 and the second chunk is 8.

Then, I would like to do a rep function and store it in a separate column. I want my data frame to look like the following:

   vec  mean
1    1     3
2    2     3
3    3     3
4    4     3
5    5     3
6    6     8
7    7     8
8    8     8
9    9     8
10  10     8

I was wondering whether a loop function would be appropriate or if there's a simpler way to go about this problem. I am open to suggestions.

3 Answers3

3

Just to add if you want to work on the split data frame here is how you can do it.

# Your vector
vec = c(1:10)

# your dataframe
df = data.frame(vec)

# Your split df 
splitdf = split(df, rep(1:2,each = 5))

# -------------------------------------------------------------------------
#initialize a list (avg) with the size of splitdf 
avg <- vector("list", length(splitdf))
# loop through each list and compute the mean and assign each to avg
for (i in seq_along(splitdf)){
  avg[[i]] <- mean(splitdf[[i]]$vec)
}
# avg
# [[1]]
# [1] 3
# 
# [[2]]
# [1] 8
# unlist avg and create a column mean on df
df$mean <- rep(unlist(avg), each=5)
# df
#     vec mean
# 1    1    3
# 2    2    3
# 3    3    3
# 4    4    3
# 5    5    3
# 6    6    8
# 7    7    8
# 8    8    8
# 9    9    8
# 10  10    8
deepseefan
  • 3,701
  • 3
  • 18
  • 31
2

No need to split the data, if you use the same logic of split as a group. For example, in ave

df$mean <- ave(df$vec, rep(1:2,each = 5)) 
df

#   vec mean
#1    1    3
#2    2    3
#3    3    3
#4    4    3
#5    5    3
#6    6    8
#7    7    8
#8    8    8
#9    9    8
#10  10    8

The default function in ave is mean already so we don't apply it explicitly here.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
1

You are probably looking for by(), which basically offers a split-apply functionality. Unsplit using rbind().

res <- do.call(rbind, 
               by(DF, rep(1:2, each=5), function(x) 
                 cbind(x, mean=colMeans(x))  # perform calculations on subsets
                 )
               )
res
#      vec mean
# 1.1    1    3
# 1.2    2    3
# 1.3    3    3
# 1.4    4    3
# 1.5    5    3
# 2.6    6    8
# 2.7    7    8
# 2.8    8    8
# 2.9    9    8
# 2.10  10    8

Data

DF <- structure(list(vec = 1:10), class = "data.frame", row.names = c(NA, 
-10L))
jay.sf
  • 60,139
  • 8
  • 53
  • 110