I need help in writing a function that checks if any column in a data frame isn't numeric and equates that column to a NULL.
Setting a column to NULL removes it from the data frame. Is your goal simply to remove non-numeric columns or is this a homework assignment for you to practice writing functions?
Yes, my goal is to remove non-numeric columns.
I am practicing the writing of functions,,it's not an assignment
I would write a function that returns either the column or NULL and then use one of the built in functions like lapply() or map() that iterate over a list. A data frame is just a list with some extra features.
MyFunc <- function(Col) {
if (is.numeric(Col)) {
return(Col)
} else {
return(NULL)
}
}
DF <- data.frame(N1 = 1:4, C1 = LETTERS[1:4],
N2 = 10:13, C2 = LETTERS[5:8])
DF
#> N1 C1 N2 C2
#> 1 1 A 10 E
#> 2 2 B 11 F
#> 3 3 C 12 G
#> 4 4 D 13 H
DF[] <- lapply(DF, MyFunc)
DF
#> N1 N2
#> 1 1 10
#> 2 2 11
#> 3 3 12
#> 4 4 13
DF <- data.frame(N1 = 1:4, C1 = LETTERS[1:4],
N2 = 10:13, C2 = LETTERS[5:8])
library(purrr)
DF <- map_dfc(DF, MyFunc)
DF
#> # A tibble: 4 x 2
#> N1 N2
#> <int> <int>
#> 1 1 10
#> 2 2 11
#> 3 3 12
#> 4 4 13
Created on 2022-05-04 by the reprex package (v2.0.1)
Thanks
kinda helped,
but I would have hoped to get a data frame or matrix because the functions I would use afterward (e.g agnes function) would not work on lists
The map_dfc function returns a tibble, which is a data frame with some particular behavior when used with tidyverse functions. You can use it as a data frame. If you run the class() function on DF, you will see that it does include data.frame. Any function that does not have methods for tbl_df or tbl will treat DF as a data.frame.
class(DF)
[1] "tbl_df" "tbl" "data.frame"
an alternative
myfunc <- function(x){
require(dplyr)
select_if(x,
~!is.numeric(.))
}
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.