# split - [R - 将一个数据集拆分为多个数据集后，如何执行计算？

``````
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

``````

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

``````

``````
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

``````

``````
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

``````

``````
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

``````

### 数据

``````
DF <- structure(list(vec = 1:10), class ="data.frame", row.names = c(NA,

-10L))

``````

``````

vec = c(1:10)

df = data.frame(vec)

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

``````