[R] Difference subsetting (dataset$variable vs. dataset["variable"]

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Tue May 31 09:28:34 CEST 2016


You were clearly mistaken. 

dataframe$column is almost the same as dataframe[["column"]], except that the $ does partial matching. Both of these "extract" a list element. 

A data frame is a list where all elements are vectors of the same length.  A list is a vector where each element can refer to any of a variety of types of objects. The names of the objects in the list are associated with the list vector, not the referred objects (e.g. columns).  The [] operator "slices" the list but keeps the names and referring semantics. The [[]] extraction operator (and its pal $) refer to a single element out of the list, losing access to the containing list and the names that go with it. 

The Introduction to R document has all this in it... it just usually glazes your eyes the first few times you read it.  You might find the R Inferno more entertaining. 

-- 
Sent from my phone. Please excuse my brevity.

On May 30, 2016 11:45:52 PM PDT, G.Maubach at weinwolf.de wrote:
>Hi All,
>
>I thought dataset$variable is the same as dataset["variable"]. I tried
>the 
>following:
>
>> str(ZWW_Kunden$Branche)
>chr [1:49673] "231" "151" "151" "231" "231" "111" "231" "111" "231"
>"231" 
>"151" "111" ...
>> str(ZWW_Kunden["Branche"])
>'data.frame':            49673 obs. of  1 variable:
> $ Branche: chr  "231" "151" "151" "231" ...
>
>and get different results: "chr {1:49673]" vs. "data.frame". First one
>is 
>a simple vector, second one is a data.frame.
>
>This has consequences when subsetting a dataset and filter cases:
>
>> ZWW_Kunden["Branche"] %in% c("315", "316", "317")
>[1] FALSE
>
>> head(ZWW_Kunden$Branche %in% c("315", "316", "317")) # head() only to
>
>shorten output
>[1] FALSE FALSE FALSE FALSE FALSE FALSE
>
>I have thought dataset$variable is the same as dataset["variable"] but 
>actually it's not.
>
>Can you explain what the difference is?
>
>Kind regards
>
>Georg
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

	[[alternative HTML version deleted]]



More information about the R-help mailing list