Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't R throw an error when I use only the initial part of my column name in a data frame?

Tags:

I have a data frame containing various columns along with sender_bank_flag. I ran the below two queries on my data frame.

sum(s_50k_sample$sender_bank_flag, na.rm=TRUE)

sum(s_50k_sample$sender_bank, na.rm=TRUE)

I got the same output from both the queries even though there is no such column as sender_bank in my data frame. I expected to get an error for the second code. Didn't know R has such a functionality! Does anyone know what exactly is this functionality & how can it be better utilized?

like image 717
Harsh Khad Avatar asked Aug 28 '18 11:08

Harsh Khad


People also ask

How do I make the first row a column name?

To promote the first row to column headers, select Home > Use First Row As Headers. To demote column headers to the first row, select Home, select the arrow next to Use First Row As Headers, and then select Use Headers as First Row.

How do I use Colnames in R?

Method 1: using colnames() methodcolnames() method in R is used to rename and replace the column names of the data frame in R. The columns of the data frame can be renamed by specifying the new column names as a vector. The new name replaces the corresponding old name of the column in the data frame.

How do I remove a specific column in R?

dplyr select() function is used to select the column and by using negation of this to remove columns.

How do I combine two columns in R?

How do I concatenate two columns in R? To concatenate two columns you can use the <code>paste()</code> function. For example, if you want to combine the two columns A and B in the dataframe df you can use the following code: <code>df['AB'] <- paste(df$A, df$B)</code>.


1 Answers

Probably worthwhile to augment all comments into an answer.


Both my comment and BenBolker's point to doc page ?Extract:

Under Recursive (list-like) objects:

Both "[[" and "$" select a single element of the list. The main difference is that "$" does not allow computed indices, whereas "[[" does. x$name is equivalent to x[["name", exact = FALSE]]. Also, the partial matching behavior of "[[" can be controlled using the exact argument.

Under Character indices:

Character indices can in some circumstances be partially matched (see ?pmatch) to the names or dimnames of the object being subsetted (but never for subassignment). Unlike S (Becker et al p. 358), R never uses partial matching when extracting by "[", and partial matching is not by default used by "[[" (see argument exact).

Thus the default behaviour is to use partial matching only when extracting from recursive objects (except environments) by "$". Even in that case, warnings can be switched on by options(warnPartialMatchDollar = TRUE).

Note, the manual has rich information, and make sure you fully digest them. I formatted the content, adding Stack Overflow threads behind where relevant.


Links provided by phiver's comment are worth reading in a long term.

like image 111
Zheyuan Li Avatar answered Oct 12 '22 07:10

Zheyuan Li