Lena
Lena

Reputation: 321

R seems to ignore part of variable name after underscore

I encountered a strange problem with R. I have a dataframe with several variables. I add a variable to this dataframe that contains an underscore, for example:

allres$tmp_weighted <- allres$day * allres$area

Before I do this, R tells me that the variable allres$tmp does not exist (which is right). However, after I add allres$tmp_weighted to the dataframe and call allres$tmp, I get the data for allres$tmp_weighted. It seems as if the part after the underscore does not matter at all for R. I tried it with several other variables / names and it always works that way

I don't think this should work like this? Am I overlooking something here? Below I pasted some code together with output from the Console.

# first check whether variable exists
allres_sw$Ndpsw

> NULL

#define new variable with underscore in variable name
allres_sw$Ndpsw_weighted <- allres_sw$Ndepswcrit * allres_sw$Area

#check again whether variable exists
allres_sw$Ndpsw

>   [1]    17.96480   217.50240    44.84415    42.14560     0.00000    43.14444    53.98650     9.81939     0.00000   110.67720

# this is the output that I would expect from "Ndpsw_weighted" - and indeed do get
allres_sw$Ndpsw_weighted
>   [1]    17.96480   217.50240    44.84415    42.14560     0.00000    43.14444    53.98650     9.81939     0.00000   110.67720

Upvotes: 2

Views: 1105

Answers (2)

Wil
Wil

Reputation: 3188

Have a look at ?`[` or ?`$` in your R console. If you look at the name argument of the extract functions it states that names are partially matched when using the $ operator (as opposed to the `[[` operator, which uses exact matches based on the exact = TRUE argument).

From ?`$`

A literal character string or a name (possibly backtick quoted). For extraction, this is normally (see under ‘Environments’) partially matched to the names of the object.

Upvotes: 2

Armali
Armali

Reputation: 19395

Just to expand somewhat on Wil's answer... From help('$'):

x$name

name
A literal character string or a name (possibly backtick quoted). For extraction, this is normally (see under ‘Environments’) partially matched to the names of the object.

x$name is equivalent to x[["name", exact = FALSE]]. Also, the partial matching behavior of [[ can be controlled using the exact argument.

exact
Controls possible partial matching of [[ when extracting by a character vector (for most objects, but see under ‘Environments’). The default is no partial matching. Value NA allows partial matching but issues a warning when it occurs. Value FALSE allows partial matching without any warning.

The key phrase here is partial match (see pmatch). You'll understand now that the underscore is nothing special - you can abbreviate allres_sw$Ndpsw_weighted to allres_sw$Ndp, provided no name is more similar than allres_sw$Ndepswcrit.

Upvotes: 1

Related Questions