Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 206970

How drop columns by CLASSES [duplicate]

$
0
0

This question already has an answer here:

I have the following data.frame:

data_1 <- structure(list(Line = structure(1:4, .Label = c("K1", "K2", "K3", 
"K4"), class = "factor"), A = c(4L, 1L, -1L, 2L), B = c(3L, -2L, 
-1L, 4L), C = c(-1L, 1L, 2L, 5L), D = c(2L, -5L, 3L, -7L), E = c(4L, 
1L, 4L, 9L)), class = "data.frame", row.names = c(NA, -4L))

I want drop integer columns. I try:

rapply(object = data_1, classes = 'integer', how = 'list', f = function(x) {
  x <- NULL
})

and

lapply(X = data_1, FUN = function(x) {
  Filter(f = is.integer, x = x) <- NULL
})

But doesn't work.

I also need to do this for a list:

list_1 <- list(data_1 = structure(list(Line = structure(1:4, .Label = c("K1", 
"K2", "K3", "K4"), class = "factor"), A = c(4L, 1L, -1L, 2L), 
B = c(3L, -2L, -1L, 4L), C = c(-1L, 1L, 2L, 5L), D = c(2L, 
-5L, 3L, -7L), E = c(4L, 1L, 4L, 9L)), class = "data.frame", row.names = c(NA, 
-4L)), data_2 = structure(list(Line = structure(1:4, .Label = c("K1", 
"K2", "K3", "K4"), class = "factor"), A = c(4L, 1L, -1L, 2L), 
B = c(3L, -2L, -1L, 4L), C = c(-1L, 1L, 2L, 5L), D = c(2L, 
-5L, 3L, -7L), E = c(4L, 1L, 4L, 9L)), class = "data.frame", row.names = c(NA, 
-4L)), data_3 = structure(list(Line = structure(1:4, .Label = c("K1", 
"K2", "K3", "K4"), class = "factor"), A = c(4L, 1L, -1L, 2L), 
B = c(3L, -2L, -1L, 4L), C = c(-1L, 1L, 2L, 5L), D = c(2L, 
-5L, 3L, -7L), E = c(4L, 1L, 4L, 9L)), class = "data.frame", row.names = c(NA, 
-4L)))

I try:

rapply(object = list_1, classes = 'integer', how = 'list', f = function(x) {
  rapply(x, rm(x))
})

lapply(X = list_1, FUN = function(x) {
  lapply(X = Filter(is.integer, x), rm(x))
})

But, also doesn't work.

Desire output:

  Line
1   K1
2   K2
3   K3
4   K4

I need a solution with R base only, not additional packages (with dplyr and your _if functions).


Viewing all articles
Browse latest Browse all 206970

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>