I would like to be able temporarily assign the columns of a data.frame
to variable names within a function.
mydata <- data.frame(a1="hello",a2=2,a3=3)
f <- function(mydata) {
for (i in names(mydata)) assign(i, tempdata[i])
print(a1)
print(a2+a3)
}
[1] "hello"
[2] 5
Thanks, I know this is another weird question but it would be helpful.
I hesitate to post this, but your comment wasn't particularly helpful in explaining what you are trying to achieve, and this seems to match the functionality of your f
function in your question.
The with
function will let you refer to column names as if they were variables:
with(mydata, {
print(a1)
print(a2 + a3)
})
# [1] hello
# Levels: hello
# [1] 5
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.