(This article was first published on

**Gregor Gorjanc**, and kindly contributed to R-bloggers)I was doing some drawing in R and I needed to trim some values to keep the data (x axis) in reasonable limits, but I did not want to loose that info. Therefore, I summed the values that would be trimmed. Since I was repeating this, I wrote a function and commited it to the gdata SVN package repository. It will probably take some time before new version of gdata hits the CRAN, so there is package for MS Windows and a source package. Here it goes in action:

> x <- 1:10

> trimSum(x, n=5)

[1] 1 2 3 4 45

> trimSum(x, n=5, right=FALSE)

[1] 21 7 8 9 10

> x[9] <- NA

> trimSum(x, n=5)

[1] 1 2 3 4 NA

> trimSum(x, n=5, na.rm=TRUE)

[1] 1 2 3 4 36

To

**leave a comment**for the author, please follow the link and comment on his blog:**Gregor Gorjanc**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...