Question

I am trying to transform a dataframe Neigh = NULL if it has just one line:

I have tried:

Neigh <- Neigh[-(which(sapply(Neigh,nrow == 1)))]

I have one loop to plot graphs, but I don´t want to plot those with just one line. That's why I am trying to make the dataframes with one line = NULL.

Was it helpful?

Solution

As Roland just said, you can use an if condition. Use it with the nrow function (which returns the number of rows of your data.frame object) and you are done.

A simple example:

df <- data.frame(1,letters[1])
if(nrow(df)==1) {
  df <- NULL
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top