user3841581
user3841581

Reputation: 2747

Using tapply to get results for a new column

I have a R dataFrame temp containing the following:

Serial N         year         current    Average 
   B              10            14          15
   C              12            13          12
   D              40            20          20
   .               .             .           .

I would like to add a new column based on each rows of the Average column. I tried using tapply as follow:

temp$new_set=tapply(temp$Average, function(x) { 2 * pnorm(x * sqrt(2)) - 1} )

But I get the following error:

r error in unique.default(x) unique() applies only to vectors

How can I apply that function on each elements of the temp$Average??

Upvotes: 1

Views: 175

Answers (1)

Andrea S.
Andrea S.

Reputation: 410

It looks like you want to calculate your function for each value of Average. If so, I do not believe you need you use tapply at all. Something along these lines would work:

temp$new_set <- (2 * pnorm(temp$Average * sqrt(2))) - 1

That is, you can pass directly the column temp$Average as the argument of your function, and obtain a result vector that can be used as your new column.

Upvotes: 4

Related Questions