Ben Marconi
Ben Marconi

Reputation: 171

Trying to get rid of a data frame row

I am trying to get rid of a data frame row. I read the data with

temp_data <- read.table(blablabla)

and then when I try to get rid of the first row with

temp_data <- temp_data[-1,]

it turns temp_data into a vector. Why is this happening?

Upvotes: 0

Views: 47

Answers (1)

akrun
akrun

Reputation: 887038

As commented by others, by default for [, it is drop=TRUE. From the ?"["

drop: For matrices and arrays. If TRUE the result is coerced to the lowest possible dimension (see the examples). This only works for extracting elements, not for the replacement. See drop for further details.

So, we need

 temp_data[-1, , drop=FALSE]

If we convert to data.table, for subsetting the rows, it is not needed,

 library(data.table)
 temp_data[-1]

data

 temp_data <- data.frame(Col1 = 1:5)

Upvotes: 1

Related Questions