0

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.

2
  • Perhaps if you explained what you were trying to achieve, it might be easier to help you with a solution. Commented Apr 12, 2014 at 10:02
  • I am working on remapping common data tasks into a more user friendly language. Commented Apr 12, 2014 at 10:05

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.