Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In R, why does selecting rows from a data frame return data as a vector if the data frame has only one column?

Tags:

dataframe

r

Suppose we want to access data from a data frame by row. The examples are simplified but when ordering a data frame by row names, for example, (df[order(row.names(df)]) we use the same technique.

If the data frame has one column, we get back an atomic vector:

> df
    x1
a   x
b   y
c   z

> df[1, ] # returns atomic vector
[1] x 

If the data frame has two columns, we get back a 1-row data frame including the row name:

> df
    x1 x2
a   x  u
b   y  v
c   z  w 

> df[1, ] # returns data frame
   X1 X2
a  x  u 

I don't understand why the same operation on the data frame yields two types of results depending on how many columns the frame has.

like image 982
malana Avatar asked Oct 06 '11 09:10

malana


People also ask

How do I select specific rows from a DataFrame in R?

By using bracket notation on R DataFrame (data.name) we can select rows by column value, by index, by name, by condition e.t.c. You can also use the R base function subset() to get the same results. Besides these, R also provides another function dplyr::filter() to get the rows from the DataFrame.

What is the difference between a vector and a data frame in R?

R vectors are used to hold multiple data values of the same datatype and are similar to arrays in C language. Data frame is a 2 dimensional table structure which is used to hold the values. In the data frame, each column contains the value of one variable and also each row contains the value of each column.

Why would you use a data frame over a vector to store your data?

One difference is that if we try to get a single row of the data frame, we get back a data frame with one row, rather than a vector. This is because the row may contain data of different types, and a vector can only hold elements of all the same type. Internally, a data frame is a list of column vectors.

How do you select rows from a DataFrame based on column values in R?

Select Rows by list of Column Values. By using the same notation you can also use an operator %in% to select the DataFrame rows based on a list of values. The following example returns all rows when state values are present in vector values c('CA','AZ','PH') .


1 Answers

It's because the default argument to [ is drop=TRUE.

From ?"["

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.

> dat1 <- data.frame(x=letters[1:3])
> dat2 <- data.frame(x=letters[1:3], y=LETTERS[1:3])

The default behaviour:

> dat[1, ]
     row sessionId scenarionName stepName duration
[1,]   1      1001             A    start        0

> dat[2, ]
     row sessionId scenarionName stepName duration
[1,]   2      1001             A    step1      2.2

Using drop=FALSE:

> dat1[1, , drop=FALSE]
  x
1 a

> dat2[1, , drop=FALSE]
  x y
1 a A
like image 83
Andrie Avatar answered Nov 04 '22 20:11

Andrie