[R] force values for elements of a dataframe below cut-off

Jim Lemon drjimlemon at gmail.com
Mon Jul 27 12:12:32 CEST 2015


Hi Luigi,
First, the way you have combined the two vectors coerces df$value to
factor. I think you want:

df<-data.frame(value,sample)

If you just want to change df$value, it's simple:

df$value[df$value<8.5]<-0

If you also want to change df$sample, you will probably want to add
another factor level and change it before you change df$value.

Jim


On Mon, Jul 27, 2015 at 7:55 PM, Luigi Marongiu
<marongiu.luigi at gmail.com> wrote:
> Dear all,
> I would like to clip the data of a dataframe by forcing the value of
> the elements below a cut-off to 0. I believe that the function to be
> used is sapply but I could not have the call working properly.
> Would you have any tip?
> Best regards
> Luigi
>
>
>>>>
> value <- c(5.43, 6.63, 0, 6.2, 5.61, 0, 5.59, 0, 0, 5.49, 18.35, 0,
> 6.07, 4.54, 4.73, 0, 5.74, 33.02, 4.45, 31.16, 0, 0, 3.12, 0, 0, 4.78,
> 0, 0, 0, 0, 0, 32.42, 3.35, 3.87, 0, 3.26, 5.75, 1.66, 0, 0, 0, 8.49,
> 3.08, 0, 0, 3.74, 0, 0, 4.06, 3.8, 0, 0
> )
> sample <- c("p.001", "p.001", "p.001", "p.001", "p.001", "p.001",
> "p.001", "p.001", "p.001", "p.001", "p.001", "p.001", "p.001",
> "p.001", "p.001", "p.001", "p.001", "p.001", "p.001", "p.001",
> "p.001", "p.001", "p.001", "p.001", "p.001", "p.001", "p.001",
> "p.001", "p.001", "p.001", "p.001", "p.001", "p.001", "p.001",
> "p.001", "p.001", "p.001", "p.001", "p.001", "p.001", "p.001",
> "p.001", "p.001", "p.001", "p.001", "p.001", "p.001", "p.001",
> "p.002", "p.002", "p.002", "p.002"
> )
>
> df <- as.data.frame(cbind(sample, value))
>
> # cut off
> co <- 8.5
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list