Reputation: 59
I created a data frame and am trying to access the columns in the data frame. The code is as shown below
df <- data.frame(n = c('A','B','C'),
mark_1 = c(23,25, 17),
mark_2 = c(45,46,50))
j <- paste('mark',1,sep="_")
j
f <- '$'(df,j)
summary(f)
n <- '$'(df,"mark_1")
summary(n)
When I run this code, this is what I get
> j <- paste('mark',1,sep="_")
> j
[1] "mark_1"
> f <- '$'(df,j)
> summary(f)
Length Class Mode
0 NULL NULL
> n <- '$'(df,"mark_1")
> summary(n)
Min. 1st Qu. Median Mean 3rd Qu. Max.
17.00 20.00 23.00 21.67 24.00 25.00
>
Why is summary(f) not working? j is properly concatenated to "mark_1". Then why is the error happening?
Thanks in advance
Upvotes: 2
Views: 156
Reputation: 263481
You could use '$' if you applied it with do.call
. do.call
is useful in situations where you have the name of the function as a character rather than a language value. In this case it also evaluates the second argument, the list of dataframe and column name.
do.call('$', list(df, j))
[1] 23 25 17
That said, I think you are better off using '[[' or '[' which do the evaluation of the second argument as a matter of course.
Upvotes: 2
Reputation: 40871
@TylerRinker showed you what to do instead.
...but the reason you can't use '$'(df,j)
is that the dollar operator uses the second argument literally.
...so '$'(df,mark_1)
would work fine. But you simply can't pass a variable to it. That's what [
and [[
are for:
df[,j] # The j'th column
df[[j]] # Pretend df is a list of columns and get the j'th element
Upvotes: 6
Reputation: 32401
'$'(df,j)
is equivalent to df$j
-- it looks at the name of the argument,
rather than its value.
You can use df[[j]]
or df[,j]
or '[['(df,j)
instead
(but avoid the last one, unless you want to make the code difficult to read).
df$j <- 1
# The "j" column
df$j
`$`(df,j)
`[[`(df,"j")
df[["j"]]
df[,"j"]
# The "mark_1" column
df[[j]]
df[,j]
`[[`(df,j)
Upvotes: 4