how to delete a column of the "list" class in R

Asked

Viewed 39 times

0

I have a data.frame in which I need to perform a correlation matrix.

I’ve already sorted the numerical data by the command select_if(is.numeric) as suggested by the colleague at link.

However, in my data.frame there is still a die with different class of "Numeric", ie, there is a class column list, which is one to sf() of a polygon of the state. I tried to apply the command select(- ) in the column list and also the command distinct_at(vars(-)) and other web tips, but did not solve. Finally I still can not make the correlation because of this list.

unfortunately I cannot reproduce an example of list here, but there is an image of part of my df.

inserir a descrição da imagem aqui

1 answer

2

The problem can be solved in R base or with the package dplyr, which is what the question seems to want.

I will use this example base:

dados <- data.frame(a = 1:3, 
                    b = I(list(1,1:2,1:3)), 
                    c = letters[1:3])
dados
#  a       b c
#1 1       1 a
#2 2    1, 2 b
#3 3 1, 2, 3 c

1. R base

First determine which class columns "numeric" and then take out those columns.

j <- sapply(dados, is.numeric)
j
#    a     b     c 
# TRUE FALSE FALSE 

dados[j]
#  a
#1 1
#2 2
#3 3

To remove only class columns "list", is done in an analogous way but denying the logical index.

i <- sapply(dados, is.list)
dados[!i]
#  a c
#1 1 a
#2 2 b
#3 3 c

2. Package dplyr

Instead of select_if, use select(where(.)).

library(dplyr)

dados %>%
  select(where(is.numeric))
#  a
#1 1
#2 2
#3 3
  • hello @Rui Barradas. I tested the solutions, however I was not successful in removing the "list" from my data frame. The clarification title I used the GEOBR package to create this data.frame. I see that the solution you put works to remove the "list" from the example dataframe, but in my case it did not work. When using the sapply, it creates a new column with False and True values. The command select(where(is.numeric)) actually separates only the Numeric data, but the column geom with the list still remains, I would use this command in another situation. But unfortunately in my case it has not worked yet.

  • @wesleysc352 Can you please, edit the question with the departure of dput(dados) or, if the base is too large, dput(head(dados, 20))?

  • ola @Rui Barradas, as my df has a list with sf() it will get huge, but I’ll put

  • ola @Rui Barradas I tried to do dput(head(data, 1)), but Stackoverflow only allows 30thousand characters and mine gave 34thousand. As I said, I used GEOBR to create my df, this package takes all the coordinates of a county boundary and puts inside the df in the format of list.

  • @wesleysc352 Then post the output of str(dados), please. Or, even better, code that allows you to create a base with similar structure but not necessarily the same size. (34,000 is too much, it’s possible much smaller?)

  • hello a little while ago I opened the question, but I managed to solve the problem in a very simple way I do not know if it applies to the other case: df2<-df %>% st_set_geometry (NULL) was able to remove columns of type list

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.