I have very wide data frame and would like to create many short data frames? It looks like this:
location = c("A","B","C","D","E","F","G","H")
V1a = c(1,5,3,2,1,2,5,6)
V2a = c(3,2,6,7,3,2,1,2)
V3a = c(1,0,5,2,2,0,2,5)
V1b = c(3,2,6,7,3,2,1,2)
V2b = c(1,5,3,2,1,2,5,6)
V3b = c(1,5,3,2,1,2,5,6)
x <- data.frame(location,V1a,V2a,V3a,V1b,V2b,V3b)
x
location V1a V2a V3a V1b V2b V3b
1 A 1 3 1 3 1 1
2 B 5 2 0 2 5 5
3 C 3 6 5 6 3 3
4 D 2 7 2 7 2 2
5 E 1 3 2 3 1 1
6 F 2 2 0 2 2 2
7 G 5 1 2 1 5 5
8 H 6 2 5 2 6 6
I would like to create from loop (if it is possible) 3 data frames as this
x1 <- x[,c(1,2,5)]
x1
location V1a V1b
1 A 1 3
2 B 5 2
3 C 3 6
4 D 2 7
5 E 1 3
6 F 2 2
7 G 5 1
8 H 6 2
x2 <- x[,c(1,3,6)]
x2
location V2a V2b
1 A 3 1
2 B 2 5
3 C 6 3
4 D 7 2
5 E 3 1
6 F 2 2
7 G 1 5
8 H 2 6
x3 <- x[,c(1,4,7)]
x3
location V3a V3b
1 A 1 1
2 B 0 5
3 C 5 3
4 D 2 2
5 E 2 1
6 F 0 2
7 G 2 5
8 H 5 6
Thank you for your help!
try this:
retis a list with 3 elements. You can access each element byret[[1]]etc.If you really want to
x1etc, then,will do the work.
But embedding index into the variable name is usually bad idea.