0
calld=data.frame(matrix(rnorm(100*50,0,1),1000,50))

for (x in names(calld)) {
   assign(paste("calld$",x,sep=""),pnorm(get(paste("calld$",x,sep="")),0,1,lower.tail=T,log.p=F))
    }
Error in get(paste("calld$", x, sep = "")) : object 'calld$X1' not found

Am I using the get function correctly?? I am trying to concatenate the names of the data set via a loop and paste of it's existing valued by passing the values through a pnorm (cumulative normal distribution function). But I keep getting an error. The function works when I call the variable names in the "calld" dataframe. The problem is the concentration process of creating the loop. Where am I going wrong? I appreciate your help

Update::

I took your advice guys and reedited the loop, to.

for (n in names(calld)) {

get("calld")[[n]]=pnorm(get("calld")[[n]],0,1,lower.tail=T,log.p=F)

}

Error in get("calld")[[n]] = pnorm(get("calld")[[n]], 0, 1, lower.tail = T,  : 
  target of assignment expands to non-language object

But now I am getting this new error. So everything on the right hand side of the equation in the loop when I tested it it works. The error arises when I set it the value equal to itself, replacing the prior values.

4
  • 1
    You cannot use paste to construct a call to $. Learn to use "[[". Commented Jun 26, 2014 at 23:13
  • 2
    And unlearn using assign/get! Commented Jun 26, 2014 at 23:16
  • the edit suggests you haven't quite got the point yet, I think ... @mnel's answer completely solves your problem. You don't need to use assign() or get() at all (and shouldn't). Commented Jun 27, 2014 at 0:44
  • Indeed, @BenBolker. There is no need for get or assign. calld[[n]] <- pnorm(calld[[n]], 0,1,TRUE,FALSE) will do what you want (but be highly inefficient as a copy of calld is made everytime you call [[<-. Only a single copy is made by my answer. Commented Jun 27, 2014 at 0:50

1 Answer 1

4

Have mercy on kittens!

You can't use assign this way, nor get.

 calld[] <- lapply(calld, pnorm, mean = 0, sd = 1)

Explanantion: calld[]<- replaces all existing columns of calld (whilst retaining the structure as a data.frame) with the results of lapply(calld, pnorm, mean = 0, sd = 1) which cycles through all columns of calld, applying pnorm on each one.

 library(fortunes)
 fortune(312)

The problem here is that the $ notation is a magical shortcut and like any other magic if used incorrectly is likely to do the programmatic equivalent of turning yourself into a toad.

-- Greg Snow (in response to a user that wanted to access a column whose name is stored in y via x$y rather than x[[y]])

R-help (February 2012)

Sign up to request clarification or add additional context in comments.

2 Comments

+1 for saving the kittens. I'll have to remember fortune(312) (or fortune("toad") if I forget the number). Maybe worth pointing out that calld[] is necessary to preserve the structure of calld; otherwise it turns into a list of vectors (rather than a data frame)
Leave those kitty's alone!! lol thank you for your help BondedDust and Mnel!!

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.