Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In R, What is the difference between df["x"] and df$x

Where can I find information on the differences between calling on a column within a data.frame via:

df <- data.frame(x=1:20,y=letters[1:20],z=20:1)

df$x
df["x"]

They both return the "same" results, but not necessarily in the same format. Another thing that I've noticed is that df$x returns a list. Whereas df["x"] returns a data.frame.

EDIT: However, knowing which one to use in which situation has become a challenge. Is there a best practice here or does it really come down to knowing what the command or function requires? So far I've just been cycling through them if my function doesn't work at first (trial and error).

like image 713
Brandon Bertelsen Avatar asked Jul 30 '10 06:07

Brandon Bertelsen


People also ask

What's the difference between DF and DF [[ ]]?

When you write df["] you are basically accessing a set of number values, but when you use df[["]] you are getting a DataFrame object which is compatible with your code.

What does DF mean in R?

A data frame is used for storing data tables. It is a list of vectors of equal length. For example, the following variable df is a data frame containing three vectors n, s, b.

What is the difference between DF column and DF [' column ']?

for setting, values, you need to use df['column'] = series . i think the subtle difference here is that when you set something with df['column'] = ser , pandas goes ahead and adds it to the columns/does some other stuff (i believe by overriding the functionality in __setitem__ . if you do df.

What is the difference between a Dataframe and a Tibble?

1. Tibble displays data along with data type while displaying whereas data frame does not. 2. Tibble fetches data using data source in its original form instead of data frame such factors, characters or numeric.


Video Answer


1 Answers

Another difference is that df$w returns NULL and df['w'] or df[['w']] gives an error with your example dataframe.

like image 78
Henrico Avatar answered Sep 22 '22 15:09

Henrico