I have a data frame that measures vertical movement of multiple objects with unique ID's. I want to rescale the X,Y coordinates from pixels to cm by using the rescale package. Each object has a minimum value of 0cm and maximum of 12.5cm, but the pixel lengths are all different because some objects were closer and some further away. I wanted to group my data by unique ID, and then rescale between 0 and 12.5cm. This is the code I used:
Data <- Data %>%
group_by(ID) %>%
rescale(Data$Y, to = c(0, 12.5), from = range(Data$Y, na.rm = TRUE, finite = TRUE))
I think I am grouping my data wrong, because I keep getting the following error:
Error in UseMethod("rescale") :
no applicable method for 'rescale' applied to an object of class "c('grouped_df', 'tbl_df', 'tbl', 'data.frame')"
Any suggestions for how to format this?
Assuming that the rescale
is from scales
, after the group_by
, rescale
it within mutate
by just specifying the column name without the Data$
. Using Data$
will extract the entire column instead of the values for each group
library(dplyr)
library(scales)
Data %>%
group_by(ID) %>%
mutate(Y = rescale(Y, to = c(0, 12.5),
from = range(Y, na.rm = TRUE, finite = TRUE)))
Using a reproducible example with mtcars
data(mtcars)
mtcars %>%
group_by(cyl) %>%
mutate(mpg = rescale(mpg, to = c(0, 12.5),
from = range(mpg, na.rm = TRUE, finite = TRUE)))