0 votes
in R Language by (1.0k points)
Slice vector in R Language

1 Answer

0 votes
by (1.0k points)

We can use lapply() or sapply() interchangeable to slice a data frame. We create a function, below_average(), that takes a vector of numerical values and returns a vector that only contains the values that are strictly above the average. We compare both results with the identical() function.

below_ave <- function(x) {  
    ave <- mean(x) 
    return(x[x > ave])
}
dt_s<- sapply(dt, below_ave)
dt_l<- lapply(dt, below_ave)
identical(dt_s, dt_l)

Output:

## [1] TRUE
Click here to read more about Loan/Mortgage
Click here to read more about Insurance

Related questions

0 votes
0 votes
asked Nov 14, 2019 in R Language by MBarbieri (1.0k points)
0 votes
asked Nov 14, 2019 in R Language by MBarbieri (1.0k points)
...