0

This is my code:

My dataframe is:

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"))

I want to create dataframes and put them in a list. The condition to create these dataframes will respect the size of the sample:

I would like to do this with dplyr package.

I did this with a for loop:

samples_size <- c(9,7,5,3)
my_samples <- vector(mode = "list", length(samples_size))

for(i in 1:length(samples_size)){

 my_samples[[i]]<-df[1:samples_size[i],]

}

my_samples

How can I do this using dplyr package?

Any help?

Thanks

5
  • 2
    There's going to be quite a bit of overlap in those "samples". Are you sure you wouldn't want to select random rows? Commented Nov 10, 2018 at 1:54
  • @42 It is just an example. My real sample is much bigger than this. Commented Nov 10, 2018 at 2:05
  • Do you want to sample rows from your original data frame or do you want to return the first n rows? I ask because that is what I believe your for loop is accomplishing Commented Nov 10, 2018 at 2:26
  • Yes. Laura. That was also what I was asking but apparently you missed it. Commented Nov 10, 2018 at 2:40
  • @HarroCyranka Yes. I want to sample rows. Commented Nov 10, 2018 at 2:41

1 Answer 1

1

Using lapply and dplyr

samples_size <- c(9,7,5,3)

list_of_df <- lapply(samples_size,function(i)df %>% sample_n(i))
list_of_df
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.