Let's say we've attached a data from a table containing column 'banana' with numeric values.
colName='banana'
Now we want the sum of that column, assuming we don't know what it's called, we only know the name is being stored in the colName variable. How do we call the sum() function? Neither does sum(as.name(colName))
nor sum(as.symbol(colName))
seem to work. noquotes()
doesn't work either.
Sample data:
colnm <- "squash"
df <- data.frame(squash=1:10, avocado="so good")
If you attach
your df
you could do this:
attach(df)
sum(get(colnm))
#[1] 55
This will do it without having to attach
:
sum(df[colnm])
#[1] 55
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.