drbunsen
drbunsen

Reputation: 10689

Rescale column in dataframe?

I have a dataframe called data where I would like to rescale the values in the 4th field to a range of 0-1000 and round the scaled value to the nearest integer. I'm trying to use ddply, round and rescale:

scaled_data <- ddply(data, round(rescale(data[,4], to=c(0,1000), from=range(data[,4], na.rm=TRUE)), 0)

The above code throws this error:

Error in `[.data.frame`(envir, exprs) : undefined columns selected

Can anyone point out the problem or a better way to accomplish what I am trying to do?

Upvotes: 1

Views: 2323

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226162

I think you're making it too complicated -- I don't see why you need ddply at all.

dd[,4] <- round(ggplot2::rescale(dd[,4],to=c(0,1000))

(I'm using ggplot2::rescale because you did, but (x-min(x))/diff(range(x))*1000 would do the same thing)

Or if you know the name of the fourth column you can:

dd <- transform(dd,fourth=rescale(fourth,to=c(0,1000)))

Upvotes: 4

Related Questions