TransWikia.com

Building a reference to a named column out of a passed vector

Stack Overflow Asked by Sherman sims on January 21, 2021

If I run a sum function and explicitly call the named column I have no problem. But if I try to build the reference out of a passed character vector I get this error.

All the answers to this error I can find talk about characters in the column itself, which is not my problem. It runs fine if I type it out. I think if has something to do with $.

Thanks in advance.

pollut<-"nitrate"

> sum(test1$nitrate, na.rm=TRUE)

[1] 67.089

> sum(paste("test1$",pollut,sep=""), na.rm=TRUE)

Error in sum(paste("test1$", pollut, sep = ""), na.rm = TRUE) :
invalid ‘type’ (character) of argument

Edit…A suggested answer did identify the problem but not the solution as Richard so clearly did.

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP