Edi Itelman
Edi Itelman

Reputation: 443

Use only numeric columns from DataFrame in a function

I have a dataframe that consists of some numeric and some non numeric columns

I tried to pipe only the numeric columns into a shapiro wilk test to see what statistical test to perform on them but I can't get it to work

CRF %>% select_if(is.numeric) %>% shapiro.test()

Returns

Error in shapiro.test(.) : is.numeric(x) is not TRUE

I think it is because at the end I am still passing a list. But when I try to deconstruct it with unlist or to just run lapply on it still won't work.

My end goal is a vector containing all the names of all the values that are non-normal according to the shapiro wilk test so I can pass it later on into the function doing the statistical analysis.

Any ideas?

Thanks

Upvotes: 0

Views: 282

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389265

Do you want to apply shapiro.test to each column separately? You can try map_if

purrr::map_if(CRF, is.numeric, shapiro.test)

Or in base R using lapply :

lapply(CRF[sapply(CRF, is.numeric)], shapiro.test)

Upvotes: 1

Related Questions