sf icon indicating copy to clipboard operation
sf copied to clipboard

Error in Apply a function on individual subsets of a sf data frame in one run using dplyr

Open MortezaAbdi opened this issue 5 years ago • 1 comments

I produce a function to remove outlier from meuse dataset!!!?

Note: I just take this data set as a work around to practice removing outlier for individual group in a sf data frame in one run by creating a new column called group(which is created based on zinc mean sample, grouping the observation to low and high zinc concentration).

Using this group, I would like to run the outliers removal function on individual group in one run using group_by in dplyr, but I am getting the following error, my gut tells me, nothing rung in the implementation!!!

Error: Problem with mutate()input..1. x Input ..1can't be recycled to size 60. i Input..1isIQR(data = meuse.sf, target = meuse.sf$zinc). i Input ..1must be size 60 or 1, not 149. i The error occured in group 1: group = "high". Runrlang::last_error() to see where the error occurred.

Here is the inter implementation.

`# Load meuse dataset

{

library(sp) data(meuse) summary(meuse) coordinates(meuse) <- ~x+y proj4string(meuse) <- CRS("+init=epsg:28992")

}

meuse.sf <- st_as_sf(meuse) st_crs(meuse.sf) <- 28992 st_crs(meuse.sf)

Create low and high zinc group based on mean zinc

group <- NA meuse.sf[(meuse.sf$zinc <= 469), 'group'] <- "low" meuse.sf[(meuse.sf$zinc > 469), 'group'] <- "high" meuse.sf$group <- as.factor(meuse.sf$group) mapview::mapview(meuse.sf, zcol= 'group')

#################### remove outliers for individual group at onece #####################

Interquartile function to remove outliers

IQR <- function(data, target)

{

IQR

get.out <- data[(target %in% boxplot(target, plot=FALSE)$out), ] get.clean <- data[!(target %in% boxplot(target, plot=FALSE)$out), ] return(get.clean)

}

######### Clean data for only low group data point

low.group <- filter(meuse.sf, group =="low") clean.low.group <- IQR(data = low.group, low.group$zinc) dim(low.group) dim(clean.low.group)

high.group <- filter(meuse.sf, group =="high") clean.high.group <- IQR(data = high.group, high.group$zinc) dim(high.group) dim(clean.high.group)

Clean individual group(subset of sf data frame) at once and .

meuse.sf.clean.all.group <- meuse.sf %>% group_by(group) %>% mutate(IQR(data = meuse.sf, target = meuse.sf$zinc)) `

MortezaAbdi avatar Aug 30 '20 21:08 MortezaAbdi

Could you please clean up the formatting of this issue and reduce it to a minimal reproducible example?

edzer avatar Sep 01 '20 07:09 edzer