Reputation: 2800
I have a list composed of 16 dataframes (dientes
). I would like to sample 80% of the rows with no replacement in each dataframe. I tried with these lapply()
functions with no success:
index <- lapply(1:nrow(dientes), sample, round(0.8*nrow), replace = F)
index <- lapply(dientes, sample, round(0.8*nrow), replace = F)
Where am I wrong?
Upvotes: 0
Views: 185
Reputation: 310
I would do the following:
index <- lapply(dientes, function(x){x[sample(x, round(0.8*nrow(x)), replace = F),]})
lapply takes the list dientes
as input
function(x){..}
applies the operation you want to each element
x
is each element and you take rows from it with x[sample(...),]
Upvotes: 1
Reputation: 8506
If index
should contain the list of sampled data.frames, you could do this:
## mock list of data.frames
dientes <- list(A=mtcars, B=iris, C=volcano)
## count input rows
lapply(dientes, nrow)
#> $A
#> [1] 32
#>
#> $B
#> [1] 150
#>
#> $C
#> [1] 87
index <- lapply(dientes, function(x) x[sample(nrow(x), round(0.8*nrow(x))), ])
## count output rows
lapply(index, nrow)
#> $A
#> [1] 26
#>
#> $B
#> [1] 120
#>
#> $C
#> [1] 70
Created on 2020-03-18 by the reprex package (v0.3.0)
Upvotes: 2