Reputation: 115
I could really use some help as a new b in R, I have a set of column data with over a thousand (1000) rows, in this form:
a <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
I want to use the data to build a neural network, and i would like it to look like this (in a form of a data frame):
x1 x2 x3 x4 y
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
6 7 8 9 10
Is there any simple way i can go about it or i will have to subset each till the end? Any help? I would be glad it the codes could have some comments for understanding, Thanks
Upvotes: 3
Views: 45
Reputation: 8413
another option is guyrot()
from wavethresh
package
library(wavethresh)
ncol <- 5
sapply(0:5, function(x) guyrot(a,-x)[1:6])
# [,1] [,2] [,3] [,4] [,5] [,6]
#[1,] 1 2 3 4 5 6
#[2,] 2 3 4 5 6 7
#[3,] 3 4 5 6 7 8
#[4,] 4 5 6 7 8 9
#[5,] 5 6 7 8 9 10
#[6,] 6 7 8 9 10 1
Upvotes: 2
Reputation: 887223
We can use
do.call(cbind, lapply(head(a,5), function(i) i:(i+5)))
Or another option is embed
as.data.frame(embed(a, 5)[,5:1])
# V1 V2 V3 V4 V5
#1 1 2 3 4 5
#2 2 3 4 5 6
#3 3 4 5 6 7
#4 4 5 6 7 8
#5 5 6 7 8 9
#6 6 7 8 9 10
Upvotes: 2