How to avoid reading data from a dataframe when the passed column name do not match exactly?

49 views Asked by At

I recently discovered that R will output data for a column name if the column name does not exist as is passed but the dataframe has a column name that meets what was passed as column name to retrieve data.

So if you have a dataframe X with column names say fruits and vegetables and if you try to retrieve data as X$fruit it will give you the fruits column data even when the passed column name (fruit) does not match the data frame column name (fruits). It throws error if there are column names like fruitss because at this time I believe R cannot decide whether to show fruits or fruitss to the passed value of x$fruit

How to avoid this?

1

There are 1 answers

0
akrun On

The $ can create confusion where there are similar prefix for column names, so it is better to use [[ or [ to extract the columns as it will match the entire string and not any partial strings.

X[["fruit"]]

Or

X[, "fruit"]