Using lapply to Sample dataframe, include new column and reorder columns
up vote
2
down vote
favorite
df<-structure(list(BBAS3 = c(22.85, 22.78, 22.8, 22.22, 22.51, 21.11,
20.84, 20.79, 20.67, 20.9, 20.95, 20.7, 21.03, 21.96, 21.9, 21.8,
21.9, 22.49, 22.65, 22.9, 22.19, 22.44, 21.66, 22.5, 22.96, 23.36,
23.64, 23.46, 23.85, 23.74, 23.9, 23.97, 23.95, 23.85, 23.66,
23.52, 23.5, 23.57, 23.28, 23.09, 23.74, 24.09, 23.96, 23.93,
23.07, 23.54, 24.04, 24.82, 24.58, 24.51, 23.88, 23.16, 23.79,
24.61, 25.12, 26, 25.87, 25.44, 25.6, 27.04, 26.8, 27.52, 27.65,
28.36, 28.77, 28.59, 28.63, 28.16, 27.58, 27.49, 27.6, 27.28,
26.95, 27.09, 27.05, 27.29, 26.74, 26.61, 26.04, 26.05, 25.68,
25.8, 25.49, 25.05, 25.33, 25.24, 25.17, 25.16, 25.11, 24.88,
25.44, 24.74, 24.78, 24.97, 25.25, 24.54, 24.64, 24.4, 23.61,
23.24, 23.1, 22.89, 23.29, 23.42, 23.03, 22.57, 22.4, 22.05,
21.88, 22.09, 21.9, 21.47, 21, 20.71, 20.71, 21.13, 20.86, 20.17,
20.46, 20.67, 21.24, 21.29, 21.54, 21.42, 21.06, 20.74, 20.29,
19.56, 19.66, 20.38, 20.93, 21.24, 20.8, 21.05, 20.82, 20.64,
20.73, 20.98, 21.14, 20.89, 20.46, 20.73, 20.56, 20.37, 19.81,
19.27, 19.41, 18.85, 18.73, 18.76, 18.91, 18.63, 19.18, 20.33,
20.31, 19.75, 19.55, 19.5, 19.34, 19.38, 19.41, 20.09, 20.74,
21.09, 21.03, 20.93, 20.5, 21.17, 21.7, 21.49, 22.08, 22.17,
22.7, 23.15, 23.03, 23.45, 24.29, 24.31, 24.2, 24.16, 24.19,
23.8, 24, 23.41, 22.73, 22.29, 22.82, 22.7, 22.96, 22.83, 23,
22.95, 22.01, 22.39, 22.54, 22.7, 22.55, 22.75, 23.25, 23.66,
24.19, 23.99, 24.03, 24.23, 24.93, 25.43, 25.15, 25.61, 25.58,
25.45, 24.92, 24.4, 24.01, 23.88, 24.43, 24.97, 24.96, 24.79,
24.3, 24.07, 23.88, 24.03, 24.21, 24.22, 23.82, 23.47, 23.8,
23.86, 23.77, 24.02, 24.3, 24.39, 23.95, 23.84, 23.59, 23.72,
23.75, 23.33, 23.16, 22.49, 22.5, 22.78, 22.4, 21.52, 21.72,
21.33, 21.61, 21.08, 21.13, 21.09, 20.94, 21.19, 21.31, 21.12,
21.52, 21.61, 21.96, 22.16, 21.88, 22.49, 22.58, 22.71, 22.96,
23.2, 23.74, 23.79, 23.65, 23.85, 23.01, 23.29, 23.65, 23.42,
23.16, 23.03, 23.3, 22.85, 22.67, 22.19, 22.14, 22.32, 22.14,
21.43, 20.35, 19.73, 19.82, 19.98, 20.28, 20.11, 20.11, 19.97,
19.55, 19.63, 19.28, 19.29, 19.15, 19.49, 19.15, 19.15, 19.13,
18.8, 18.28, 18.36, 18.31, 18.28, 18.41, 18.43, 18.07, 18.22,
18.66, 19.53, 20.01, 19.52, 20.08, 18.85, 19.06, 19.04, 18.94,
19.31, 18.94, 19.14, 19.74, 19.74, 19.92, 20.04, 19.83, 20.09,
20.1, 19.52, 19.81, 19.83, 20.44, 21.33, 21.6, 21.83, 21.75,
21.57, 22.44, 22.41, 22.27, 22.98, 23.07, 23.22, 23.01, 22.56,
22.91, 23.05, 22.65, 22.19, 21.71, 21.55, 21.34, 20.82, 20.11,
20.21, 19.93, 20.11, 20.35, 20.69, 20.24, 19.8, 19.87, 20.17,
19.93, 19.99, 20.19, 20.14, 19.8, 19.22, 20.04, 20.68, 20.01,
20.43, 20.14, 20.38, 19.78, 18.89, 18.99, 18.65, 18.82, 18.5,
17.61, 16.47, 16.85, 16.72, 17.04, 17.02, 16.65, 15.57, 15.69,
16.08, 15.93, 15.87, 16.15, 16.63, 17.02, 17.06, 17.06, 16.81,
16.7, 16.05, 16.08, 15.95, 16.34, 17, 17.27, 17.43, 17.5, 16.81,
16.17, 16.01, 16.31, 16.35, 16.35, 16.2, 16.18, 15.85, 16.25,
16.82, 16.86, 16.79, 17.15, 17.37, 17.53, 17.38, 17.29, 18.05,
18.01, 16.98, 16.35, 17.19, 17.3, 17.95, 18.76, 18.77, 18.78,
19.26, 19.22, 19.05, 19, 19.28, 19.82, 19.79, 19.54, 19.36, 19.84,
19.99, 20.26, 20.03, 19.97, 19.65, 20.1, 20.59, 21.16, 20.67,
20.36, 20.24, 20.28, 20.34, 21.62, 21.73)), row.names = c(NA,
-460L), class = c("tbl_df", "tbl", "data.frame"))
With this data frame I create a list of dataframe with the code bellow:
samples_size <- c(9,7,5,3)
list_of_df <- lapply(samples_size,function(i)df %>% slice(1:i))
list_of_df
Now I have this vector bellow which will be my new column to included in each dataframe of the list list_of_df
respecting the sample size:
time=seq(samples_size[i],1)
The Final result should be a list of dataframes like this:
list_of_df[[1]]<-cbind(list_of_df[[1]],seq(samples_size[1],1))
list_of_df[[1]]<-cbind(list_of_df[[2]],seq(samples_size[2],1))
list_of_df[[1]]<-cbind(list_of_df[[3]],seq(samples_size[3],1))
list_of_df[[1]]<-cbind(list_of_df[[4]],seq(samples_size[4],1))
And then, after this I would like to put the new collumn in the first position.
Is it possible to do all of these steps by using the first code above with lapply
function :
list_of_df <- lapply(samples_size,function(i)df %>% slice(1:i))
and using dplyr
Package??
r dplyr lapply
add a comment |
up vote
2
down vote
favorite
df<-structure(list(BBAS3 = c(22.85, 22.78, 22.8, 22.22, 22.51, 21.11,
20.84, 20.79, 20.67, 20.9, 20.95, 20.7, 21.03, 21.96, 21.9, 21.8,
21.9, 22.49, 22.65, 22.9, 22.19, 22.44, 21.66, 22.5, 22.96, 23.36,
23.64, 23.46, 23.85, 23.74, 23.9, 23.97, 23.95, 23.85, 23.66,
23.52, 23.5, 23.57, 23.28, 23.09, 23.74, 24.09, 23.96, 23.93,
23.07, 23.54, 24.04, 24.82, 24.58, 24.51, 23.88, 23.16, 23.79,
24.61, 25.12, 26, 25.87, 25.44, 25.6, 27.04, 26.8, 27.52, 27.65,
28.36, 28.77, 28.59, 28.63, 28.16, 27.58, 27.49, 27.6, 27.28,
26.95, 27.09, 27.05, 27.29, 26.74, 26.61, 26.04, 26.05, 25.68,
25.8, 25.49, 25.05, 25.33, 25.24, 25.17, 25.16, 25.11, 24.88,
25.44, 24.74, 24.78, 24.97, 25.25, 24.54, 24.64, 24.4, 23.61,
23.24, 23.1, 22.89, 23.29, 23.42, 23.03, 22.57, 22.4, 22.05,
21.88, 22.09, 21.9, 21.47, 21, 20.71, 20.71, 21.13, 20.86, 20.17,
20.46, 20.67, 21.24, 21.29, 21.54, 21.42, 21.06, 20.74, 20.29,
19.56, 19.66, 20.38, 20.93, 21.24, 20.8, 21.05, 20.82, 20.64,
20.73, 20.98, 21.14, 20.89, 20.46, 20.73, 20.56, 20.37, 19.81,
19.27, 19.41, 18.85, 18.73, 18.76, 18.91, 18.63, 19.18, 20.33,
20.31, 19.75, 19.55, 19.5, 19.34, 19.38, 19.41, 20.09, 20.74,
21.09, 21.03, 20.93, 20.5, 21.17, 21.7, 21.49, 22.08, 22.17,
22.7, 23.15, 23.03, 23.45, 24.29, 24.31, 24.2, 24.16, 24.19,
23.8, 24, 23.41, 22.73, 22.29, 22.82, 22.7, 22.96, 22.83, 23,
22.95, 22.01, 22.39, 22.54, 22.7, 22.55, 22.75, 23.25, 23.66,
24.19, 23.99, 24.03, 24.23, 24.93, 25.43, 25.15, 25.61, 25.58,
25.45, 24.92, 24.4, 24.01, 23.88, 24.43, 24.97, 24.96, 24.79,
24.3, 24.07, 23.88, 24.03, 24.21, 24.22, 23.82, 23.47, 23.8,
23.86, 23.77, 24.02, 24.3, 24.39, 23.95, 23.84, 23.59, 23.72,
23.75, 23.33, 23.16, 22.49, 22.5, 22.78, 22.4, 21.52, 21.72,
21.33, 21.61, 21.08, 21.13, 21.09, 20.94, 21.19, 21.31, 21.12,
21.52, 21.61, 21.96, 22.16, 21.88, 22.49, 22.58, 22.71, 22.96,
23.2, 23.74, 23.79, 23.65, 23.85, 23.01, 23.29, 23.65, 23.42,
23.16, 23.03, 23.3, 22.85, 22.67, 22.19, 22.14, 22.32, 22.14,
21.43, 20.35, 19.73, 19.82, 19.98, 20.28, 20.11, 20.11, 19.97,
19.55, 19.63, 19.28, 19.29, 19.15, 19.49, 19.15, 19.15, 19.13,
18.8, 18.28, 18.36, 18.31, 18.28, 18.41, 18.43, 18.07, 18.22,
18.66, 19.53, 20.01, 19.52, 20.08, 18.85, 19.06, 19.04, 18.94,
19.31, 18.94, 19.14, 19.74, 19.74, 19.92, 20.04, 19.83, 20.09,
20.1, 19.52, 19.81, 19.83, 20.44, 21.33, 21.6, 21.83, 21.75,
21.57, 22.44, 22.41, 22.27, 22.98, 23.07, 23.22, 23.01, 22.56,
22.91, 23.05, 22.65, 22.19, 21.71, 21.55, 21.34, 20.82, 20.11,
20.21, 19.93, 20.11, 20.35, 20.69, 20.24, 19.8, 19.87, 20.17,
19.93, 19.99, 20.19, 20.14, 19.8, 19.22, 20.04, 20.68, 20.01,
20.43, 20.14, 20.38, 19.78, 18.89, 18.99, 18.65, 18.82, 18.5,
17.61, 16.47, 16.85, 16.72, 17.04, 17.02, 16.65, 15.57, 15.69,
16.08, 15.93, 15.87, 16.15, 16.63, 17.02, 17.06, 17.06, 16.81,
16.7, 16.05, 16.08, 15.95, 16.34, 17, 17.27, 17.43, 17.5, 16.81,
16.17, 16.01, 16.31, 16.35, 16.35, 16.2, 16.18, 15.85, 16.25,
16.82, 16.86, 16.79, 17.15, 17.37, 17.53, 17.38, 17.29, 18.05,
18.01, 16.98, 16.35, 17.19, 17.3, 17.95, 18.76, 18.77, 18.78,
19.26, 19.22, 19.05, 19, 19.28, 19.82, 19.79, 19.54, 19.36, 19.84,
19.99, 20.26, 20.03, 19.97, 19.65, 20.1, 20.59, 21.16, 20.67,
20.36, 20.24, 20.28, 20.34, 21.62, 21.73)), row.names = c(NA,
-460L), class = c("tbl_df", "tbl", "data.frame"))
With this data frame I create a list of dataframe with the code bellow:
samples_size <- c(9,7,5,3)
list_of_df <- lapply(samples_size,function(i)df %>% slice(1:i))
list_of_df
Now I have this vector bellow which will be my new column to included in each dataframe of the list list_of_df
respecting the sample size:
time=seq(samples_size[i],1)
The Final result should be a list of dataframes like this:
list_of_df[[1]]<-cbind(list_of_df[[1]],seq(samples_size[1],1))
list_of_df[[1]]<-cbind(list_of_df[[2]],seq(samples_size[2],1))
list_of_df[[1]]<-cbind(list_of_df[[3]],seq(samples_size[3],1))
list_of_df[[1]]<-cbind(list_of_df[[4]],seq(samples_size[4],1))
And then, after this I would like to put the new collumn in the first position.
Is it possible to do all of these steps by using the first code above with lapply
function :
list_of_df <- lapply(samples_size,function(i)df %>% slice(1:i))
and using dplyr
Package??
r dplyr lapply
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
df<-structure(list(BBAS3 = c(22.85, 22.78, 22.8, 22.22, 22.51, 21.11,
20.84, 20.79, 20.67, 20.9, 20.95, 20.7, 21.03, 21.96, 21.9, 21.8,
21.9, 22.49, 22.65, 22.9, 22.19, 22.44, 21.66, 22.5, 22.96, 23.36,
23.64, 23.46, 23.85, 23.74, 23.9, 23.97, 23.95, 23.85, 23.66,
23.52, 23.5, 23.57, 23.28, 23.09, 23.74, 24.09, 23.96, 23.93,
23.07, 23.54, 24.04, 24.82, 24.58, 24.51, 23.88, 23.16, 23.79,
24.61, 25.12, 26, 25.87, 25.44, 25.6, 27.04, 26.8, 27.52, 27.65,
28.36, 28.77, 28.59, 28.63, 28.16, 27.58, 27.49, 27.6, 27.28,
26.95, 27.09, 27.05, 27.29, 26.74, 26.61, 26.04, 26.05, 25.68,
25.8, 25.49, 25.05, 25.33, 25.24, 25.17, 25.16, 25.11, 24.88,
25.44, 24.74, 24.78, 24.97, 25.25, 24.54, 24.64, 24.4, 23.61,
23.24, 23.1, 22.89, 23.29, 23.42, 23.03, 22.57, 22.4, 22.05,
21.88, 22.09, 21.9, 21.47, 21, 20.71, 20.71, 21.13, 20.86, 20.17,
20.46, 20.67, 21.24, 21.29, 21.54, 21.42, 21.06, 20.74, 20.29,
19.56, 19.66, 20.38, 20.93, 21.24, 20.8, 21.05, 20.82, 20.64,
20.73, 20.98, 21.14, 20.89, 20.46, 20.73, 20.56, 20.37, 19.81,
19.27, 19.41, 18.85, 18.73, 18.76, 18.91, 18.63, 19.18, 20.33,
20.31, 19.75, 19.55, 19.5, 19.34, 19.38, 19.41, 20.09, 20.74,
21.09, 21.03, 20.93, 20.5, 21.17, 21.7, 21.49, 22.08, 22.17,
22.7, 23.15, 23.03, 23.45, 24.29, 24.31, 24.2, 24.16, 24.19,
23.8, 24, 23.41, 22.73, 22.29, 22.82, 22.7, 22.96, 22.83, 23,
22.95, 22.01, 22.39, 22.54, 22.7, 22.55, 22.75, 23.25, 23.66,
24.19, 23.99, 24.03, 24.23, 24.93, 25.43, 25.15, 25.61, 25.58,
25.45, 24.92, 24.4, 24.01, 23.88, 24.43, 24.97, 24.96, 24.79,
24.3, 24.07, 23.88, 24.03, 24.21, 24.22, 23.82, 23.47, 23.8,
23.86, 23.77, 24.02, 24.3, 24.39, 23.95, 23.84, 23.59, 23.72,
23.75, 23.33, 23.16, 22.49, 22.5, 22.78, 22.4, 21.52, 21.72,
21.33, 21.61, 21.08, 21.13, 21.09, 20.94, 21.19, 21.31, 21.12,
21.52, 21.61, 21.96, 22.16, 21.88, 22.49, 22.58, 22.71, 22.96,
23.2, 23.74, 23.79, 23.65, 23.85, 23.01, 23.29, 23.65, 23.42,
23.16, 23.03, 23.3, 22.85, 22.67, 22.19, 22.14, 22.32, 22.14,
21.43, 20.35, 19.73, 19.82, 19.98, 20.28, 20.11, 20.11, 19.97,
19.55, 19.63, 19.28, 19.29, 19.15, 19.49, 19.15, 19.15, 19.13,
18.8, 18.28, 18.36, 18.31, 18.28, 18.41, 18.43, 18.07, 18.22,
18.66, 19.53, 20.01, 19.52, 20.08, 18.85, 19.06, 19.04, 18.94,
19.31, 18.94, 19.14, 19.74, 19.74, 19.92, 20.04, 19.83, 20.09,
20.1, 19.52, 19.81, 19.83, 20.44, 21.33, 21.6, 21.83, 21.75,
21.57, 22.44, 22.41, 22.27, 22.98, 23.07, 23.22, 23.01, 22.56,
22.91, 23.05, 22.65, 22.19, 21.71, 21.55, 21.34, 20.82, 20.11,
20.21, 19.93, 20.11, 20.35, 20.69, 20.24, 19.8, 19.87, 20.17,
19.93, 19.99, 20.19, 20.14, 19.8, 19.22, 20.04, 20.68, 20.01,
20.43, 20.14, 20.38, 19.78, 18.89, 18.99, 18.65, 18.82, 18.5,
17.61, 16.47, 16.85, 16.72, 17.04, 17.02, 16.65, 15.57, 15.69,
16.08, 15.93, 15.87, 16.15, 16.63, 17.02, 17.06, 17.06, 16.81,
16.7, 16.05, 16.08, 15.95, 16.34, 17, 17.27, 17.43, 17.5, 16.81,
16.17, 16.01, 16.31, 16.35, 16.35, 16.2, 16.18, 15.85, 16.25,
16.82, 16.86, 16.79, 17.15, 17.37, 17.53, 17.38, 17.29, 18.05,
18.01, 16.98, 16.35, 17.19, 17.3, 17.95, 18.76, 18.77, 18.78,
19.26, 19.22, 19.05, 19, 19.28, 19.82, 19.79, 19.54, 19.36, 19.84,
19.99, 20.26, 20.03, 19.97, 19.65, 20.1, 20.59, 21.16, 20.67,
20.36, 20.24, 20.28, 20.34, 21.62, 21.73)), row.names = c(NA,
-460L), class = c("tbl_df", "tbl", "data.frame"))
With this data frame I create a list of dataframe with the code bellow:
samples_size <- c(9,7,5,3)
list_of_df <- lapply(samples_size,function(i)df %>% slice(1:i))
list_of_df
Now I have this vector bellow which will be my new column to included in each dataframe of the list list_of_df
respecting the sample size:
time=seq(samples_size[i],1)
The Final result should be a list of dataframes like this:
list_of_df[[1]]<-cbind(list_of_df[[1]],seq(samples_size[1],1))
list_of_df[[1]]<-cbind(list_of_df[[2]],seq(samples_size[2],1))
list_of_df[[1]]<-cbind(list_of_df[[3]],seq(samples_size[3],1))
list_of_df[[1]]<-cbind(list_of_df[[4]],seq(samples_size[4],1))
And then, after this I would like to put the new collumn in the first position.
Is it possible to do all of these steps by using the first code above with lapply
function :
list_of_df <- lapply(samples_size,function(i)df %>% slice(1:i))
and using dplyr
Package??
r dplyr lapply
df<-structure(list(BBAS3 = c(22.85, 22.78, 22.8, 22.22, 22.51, 21.11,
20.84, 20.79, 20.67, 20.9, 20.95, 20.7, 21.03, 21.96, 21.9, 21.8,
21.9, 22.49, 22.65, 22.9, 22.19, 22.44, 21.66, 22.5, 22.96, 23.36,
23.64, 23.46, 23.85, 23.74, 23.9, 23.97, 23.95, 23.85, 23.66,
23.52, 23.5, 23.57, 23.28, 23.09, 23.74, 24.09, 23.96, 23.93,
23.07, 23.54, 24.04, 24.82, 24.58, 24.51, 23.88, 23.16, 23.79,
24.61, 25.12, 26, 25.87, 25.44, 25.6, 27.04, 26.8, 27.52, 27.65,
28.36, 28.77, 28.59, 28.63, 28.16, 27.58, 27.49, 27.6, 27.28,
26.95, 27.09, 27.05, 27.29, 26.74, 26.61, 26.04, 26.05, 25.68,
25.8, 25.49, 25.05, 25.33, 25.24, 25.17, 25.16, 25.11, 24.88,
25.44, 24.74, 24.78, 24.97, 25.25, 24.54, 24.64, 24.4, 23.61,
23.24, 23.1, 22.89, 23.29, 23.42, 23.03, 22.57, 22.4, 22.05,
21.88, 22.09, 21.9, 21.47, 21, 20.71, 20.71, 21.13, 20.86, 20.17,
20.46, 20.67, 21.24, 21.29, 21.54, 21.42, 21.06, 20.74, 20.29,
19.56, 19.66, 20.38, 20.93, 21.24, 20.8, 21.05, 20.82, 20.64,
20.73, 20.98, 21.14, 20.89, 20.46, 20.73, 20.56, 20.37, 19.81,
19.27, 19.41, 18.85, 18.73, 18.76, 18.91, 18.63, 19.18, 20.33,
20.31, 19.75, 19.55, 19.5, 19.34, 19.38, 19.41, 20.09, 20.74,
21.09, 21.03, 20.93, 20.5, 21.17, 21.7, 21.49, 22.08, 22.17,
22.7, 23.15, 23.03, 23.45, 24.29, 24.31, 24.2, 24.16, 24.19,
23.8, 24, 23.41, 22.73, 22.29, 22.82, 22.7, 22.96, 22.83, 23,
22.95, 22.01, 22.39, 22.54, 22.7, 22.55, 22.75, 23.25, 23.66,
24.19, 23.99, 24.03, 24.23, 24.93, 25.43, 25.15, 25.61, 25.58,
25.45, 24.92, 24.4, 24.01, 23.88, 24.43, 24.97, 24.96, 24.79,
24.3, 24.07, 23.88, 24.03, 24.21, 24.22, 23.82, 23.47, 23.8,
23.86, 23.77, 24.02, 24.3, 24.39, 23.95, 23.84, 23.59, 23.72,
23.75, 23.33, 23.16, 22.49, 22.5, 22.78, 22.4, 21.52, 21.72,
21.33, 21.61, 21.08, 21.13, 21.09, 20.94, 21.19, 21.31, 21.12,
21.52, 21.61, 21.96, 22.16, 21.88, 22.49, 22.58, 22.71, 22.96,
23.2, 23.74, 23.79, 23.65, 23.85, 23.01, 23.29, 23.65, 23.42,
23.16, 23.03, 23.3, 22.85, 22.67, 22.19, 22.14, 22.32, 22.14,
21.43, 20.35, 19.73, 19.82, 19.98, 20.28, 20.11, 20.11, 19.97,
19.55, 19.63, 19.28, 19.29, 19.15, 19.49, 19.15, 19.15, 19.13,
18.8, 18.28, 18.36, 18.31, 18.28, 18.41, 18.43, 18.07, 18.22,
18.66, 19.53, 20.01, 19.52, 20.08, 18.85, 19.06, 19.04, 18.94,
19.31, 18.94, 19.14, 19.74, 19.74, 19.92, 20.04, 19.83, 20.09,
20.1, 19.52, 19.81, 19.83, 20.44, 21.33, 21.6, 21.83, 21.75,
21.57, 22.44, 22.41, 22.27, 22.98, 23.07, 23.22, 23.01, 22.56,
22.91, 23.05, 22.65, 22.19, 21.71, 21.55, 21.34, 20.82, 20.11,
20.21, 19.93, 20.11, 20.35, 20.69, 20.24, 19.8, 19.87, 20.17,
19.93, 19.99, 20.19, 20.14, 19.8, 19.22, 20.04, 20.68, 20.01,
20.43, 20.14, 20.38, 19.78, 18.89, 18.99, 18.65, 18.82, 18.5,
17.61, 16.47, 16.85, 16.72, 17.04, 17.02, 16.65, 15.57, 15.69,
16.08, 15.93, 15.87, 16.15, 16.63, 17.02, 17.06, 17.06, 16.81,
16.7, 16.05, 16.08, 15.95, 16.34, 17, 17.27, 17.43, 17.5, 16.81,
16.17, 16.01, 16.31, 16.35, 16.35, 16.2, 16.18, 15.85, 16.25,
16.82, 16.86, 16.79, 17.15, 17.37, 17.53, 17.38, 17.29, 18.05,
18.01, 16.98, 16.35, 17.19, 17.3, 17.95, 18.76, 18.77, 18.78,
19.26, 19.22, 19.05, 19, 19.28, 19.82, 19.79, 19.54, 19.36, 19.84,
19.99, 20.26, 20.03, 19.97, 19.65, 20.1, 20.59, 21.16, 20.67,
20.36, 20.24, 20.28, 20.34, 21.62, 21.73)), row.names = c(NA,
-460L), class = c("tbl_df", "tbl", "data.frame"))
With this data frame I create a list of dataframe with the code bellow:
samples_size <- c(9,7,5,3)
list_of_df <- lapply(samples_size,function(i)df %>% slice(1:i))
list_of_df
Now I have this vector bellow which will be my new column to included in each dataframe of the list list_of_df
respecting the sample size:
time=seq(samples_size[i],1)
The Final result should be a list of dataframes like this:
list_of_df[[1]]<-cbind(list_of_df[[1]],seq(samples_size[1],1))
list_of_df[[1]]<-cbind(list_of_df[[2]],seq(samples_size[2],1))
list_of_df[[1]]<-cbind(list_of_df[[3]],seq(samples_size[3],1))
list_of_df[[1]]<-cbind(list_of_df[[4]],seq(samples_size[4],1))
And then, after this I would like to put the new collumn in the first position.
Is it possible to do all of these steps by using the first code above with lapply
function :
list_of_df <- lapply(samples_size,function(i)df %>% slice(1:i))
and using dplyr
Package??
r dplyr lapply
r dplyr lapply
asked Nov 10 at 14:43
Laura
33919
33919
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
Yes, and there is no need for dplyr
:
lapply(samples_size, function(i) cbind(time = i:1, df[1:i, ]))
For instance, the first element will be
# [[1]]
# time BBAS3
# 1 9 22.85
# 2 8 22.78
# 3 7 22.80
# 4 6 22.22
# 5 5 22.51
# 6 4 21.11
# 7 3 20.84
# 8 2 20.79
# 9 1 20.67
As I was saying in the comment, lapply
takes elements of sample_size
one by one, in this case they are called i
, and the manually defined anonymous function constructs some kind of result. Then all the results are returned a a list (contrary to sapply
, which would try to merge the results to, say, a vector). So then this time
column is simply i:1
, which is a short way to write seq(i, 1)
, cbind
stands for column which, which is what you are after, and by putting time = i:1
we make it as the first column in every intermediate result. Lastly, df[1:i, ]
gives the first i
rows of df
, and, in this case, is a simple base R analogue of slice
.
Amazing! I have to study deeply lapply function. Thanks @Julius Vainora
– Laura
Nov 10 at 14:58
1
@Laura, in every steplapply
takes a single element fromsamples_size
and the you can do with it whatever you want. Lastly, the ultimate result is a list of those intermediate results. So, rather than justlapply
, I'd say studying base R (cbind
,i:1
, etc.) is indeed a good idea before trying to usedplyr
in every step.
– Julius Vainora
Nov 10 at 15:02
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Yes, and there is no need for dplyr
:
lapply(samples_size, function(i) cbind(time = i:1, df[1:i, ]))
For instance, the first element will be
# [[1]]
# time BBAS3
# 1 9 22.85
# 2 8 22.78
# 3 7 22.80
# 4 6 22.22
# 5 5 22.51
# 6 4 21.11
# 7 3 20.84
# 8 2 20.79
# 9 1 20.67
As I was saying in the comment, lapply
takes elements of sample_size
one by one, in this case they are called i
, and the manually defined anonymous function constructs some kind of result. Then all the results are returned a a list (contrary to sapply
, which would try to merge the results to, say, a vector). So then this time
column is simply i:1
, which is a short way to write seq(i, 1)
, cbind
stands for column which, which is what you are after, and by putting time = i:1
we make it as the first column in every intermediate result. Lastly, df[1:i, ]
gives the first i
rows of df
, and, in this case, is a simple base R analogue of slice
.
Amazing! I have to study deeply lapply function. Thanks @Julius Vainora
– Laura
Nov 10 at 14:58
1
@Laura, in every steplapply
takes a single element fromsamples_size
and the you can do with it whatever you want. Lastly, the ultimate result is a list of those intermediate results. So, rather than justlapply
, I'd say studying base R (cbind
,i:1
, etc.) is indeed a good idea before trying to usedplyr
in every step.
– Julius Vainora
Nov 10 at 15:02
add a comment |
up vote
3
down vote
accepted
Yes, and there is no need for dplyr
:
lapply(samples_size, function(i) cbind(time = i:1, df[1:i, ]))
For instance, the first element will be
# [[1]]
# time BBAS3
# 1 9 22.85
# 2 8 22.78
# 3 7 22.80
# 4 6 22.22
# 5 5 22.51
# 6 4 21.11
# 7 3 20.84
# 8 2 20.79
# 9 1 20.67
As I was saying in the comment, lapply
takes elements of sample_size
one by one, in this case they are called i
, and the manually defined anonymous function constructs some kind of result. Then all the results are returned a a list (contrary to sapply
, which would try to merge the results to, say, a vector). So then this time
column is simply i:1
, which is a short way to write seq(i, 1)
, cbind
stands for column which, which is what you are after, and by putting time = i:1
we make it as the first column in every intermediate result. Lastly, df[1:i, ]
gives the first i
rows of df
, and, in this case, is a simple base R analogue of slice
.
Amazing! I have to study deeply lapply function. Thanks @Julius Vainora
– Laura
Nov 10 at 14:58
1
@Laura, in every steplapply
takes a single element fromsamples_size
and the you can do with it whatever you want. Lastly, the ultimate result is a list of those intermediate results. So, rather than justlapply
, I'd say studying base R (cbind
,i:1
, etc.) is indeed a good idea before trying to usedplyr
in every step.
– Julius Vainora
Nov 10 at 15:02
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Yes, and there is no need for dplyr
:
lapply(samples_size, function(i) cbind(time = i:1, df[1:i, ]))
For instance, the first element will be
# [[1]]
# time BBAS3
# 1 9 22.85
# 2 8 22.78
# 3 7 22.80
# 4 6 22.22
# 5 5 22.51
# 6 4 21.11
# 7 3 20.84
# 8 2 20.79
# 9 1 20.67
As I was saying in the comment, lapply
takes elements of sample_size
one by one, in this case they are called i
, and the manually defined anonymous function constructs some kind of result. Then all the results are returned a a list (contrary to sapply
, which would try to merge the results to, say, a vector). So then this time
column is simply i:1
, which is a short way to write seq(i, 1)
, cbind
stands for column which, which is what you are after, and by putting time = i:1
we make it as the first column in every intermediate result. Lastly, df[1:i, ]
gives the first i
rows of df
, and, in this case, is a simple base R analogue of slice
.
Yes, and there is no need for dplyr
:
lapply(samples_size, function(i) cbind(time = i:1, df[1:i, ]))
For instance, the first element will be
# [[1]]
# time BBAS3
# 1 9 22.85
# 2 8 22.78
# 3 7 22.80
# 4 6 22.22
# 5 5 22.51
# 6 4 21.11
# 7 3 20.84
# 8 2 20.79
# 9 1 20.67
As I was saying in the comment, lapply
takes elements of sample_size
one by one, in this case they are called i
, and the manually defined anonymous function constructs some kind of result. Then all the results are returned a a list (contrary to sapply
, which would try to merge the results to, say, a vector). So then this time
column is simply i:1
, which is a short way to write seq(i, 1)
, cbind
stands for column which, which is what you are after, and by putting time = i:1
we make it as the first column in every intermediate result. Lastly, df[1:i, ]
gives the first i
rows of df
, and, in this case, is a simple base R analogue of slice
.
edited Nov 10 at 15:16
answered Nov 10 at 14:54
Julius Vainora
26.3k75877
26.3k75877
Amazing! I have to study deeply lapply function. Thanks @Julius Vainora
– Laura
Nov 10 at 14:58
1
@Laura, in every steplapply
takes a single element fromsamples_size
and the you can do with it whatever you want. Lastly, the ultimate result is a list of those intermediate results. So, rather than justlapply
, I'd say studying base R (cbind
,i:1
, etc.) is indeed a good idea before trying to usedplyr
in every step.
– Julius Vainora
Nov 10 at 15:02
add a comment |
Amazing! I have to study deeply lapply function. Thanks @Julius Vainora
– Laura
Nov 10 at 14:58
1
@Laura, in every steplapply
takes a single element fromsamples_size
and the you can do with it whatever you want. Lastly, the ultimate result is a list of those intermediate results. So, rather than justlapply
, I'd say studying base R (cbind
,i:1
, etc.) is indeed a good idea before trying to usedplyr
in every step.
– Julius Vainora
Nov 10 at 15:02
Amazing! I have to study deeply lapply function. Thanks @Julius Vainora
– Laura
Nov 10 at 14:58
Amazing! I have to study deeply lapply function. Thanks @Julius Vainora
– Laura
Nov 10 at 14:58
1
1
@Laura, in every step
lapply
takes a single element from samples_size
and the you can do with it whatever you want. Lastly, the ultimate result is a list of those intermediate results. So, rather than just lapply
, I'd say studying base R (cbind
, i:1
, etc.) is indeed a good idea before trying to use dplyr
in every step.– Julius Vainora
Nov 10 at 15:02
@Laura, in every step
lapply
takes a single element from samples_size
and the you can do with it whatever you want. Lastly, the ultimate result is a list of those intermediate results. So, rather than just lapply
, I'd say studying base R (cbind
, i:1
, etc.) is indeed a good idea before trying to use dplyr
in every step.– Julius Vainora
Nov 10 at 15:02
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240048%2fusing-lapply-to-sample-dataframe-include-new-column-and-reorder-columns%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password