Paul Bernal
2022-May-09 08:23 UTC
[R] Filtering an Entire Dataset based on Several Conditions
Dear friends, I have a dataframe which every single (i,j) entry (i standing for ith row, j for jth column) has been normalized (converted to z-scores). Now I want to filter or subset the dataframe so that I only end up with a a dataframe containing only entries greater than -3 or less than 3. How could I accomplish this? Best, Paul [[alternative HTML version deleted]]
Eric Berger
2022-May-09 08:48 UTC
[R] Filtering an Entire Dataset based on Several Conditions
Hi Paul, Not completely clear what you are looking for. Please create a 3 x 3 example data frame with dummy data and show what you want the result to be. On Mon, May 9, 2022 at 11:24 AM Paul Bernal <paulbernal07 at gmail.com> wrote:> Dear friends, > > I have a dataframe which every single (i,j) entry (i standing for ith row, > j for jth column) has been normalized (converted to z-scores). > > Now I want to filter or subset the dataframe so that I only end up with a a > dataframe containing only entries greater than -3 or less than 3. > > How could I accomplish this? > > Best, > Paul > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
Jim Lemon
2022-May-09 09:11 UTC
[R] Filtering an Entire Dataset based on Several Conditions
Hi Paul,
Based on my guess that all values have been normalized, I would say:
mat<-(matrix(runif(16,-5,5),4))
df<-as.data.frame(mat)
df[abs(df) < 3]<-NA
df
V1 V2 V3 V4
1 NA 4.675699 3.166625 NA
2 NA NA NA 3.463660
3 4.288831 NA 4.032902 -4.343869
4 NA NA NA NA
Beware that this behavior of data frames may not persist in the
future. You may have to convert to a matrix and then convert the
result back again.
Jim
On Mon, May 9, 2022 at 6:24 PM Paul Bernal <paulbernal07 at gmail.com>
wrote:>
> Dear friends,
>
> I have a dataframe which every single (i,j) entry (i standing for ith row,
> j for jth column) has been normalized (converted to z-scores).
>
> Now I want to filter or subset the dataframe so that I only end up with a a
> dataframe containing only entries greater than -3 or less than 3.
>
> How could I accomplish this?
>
> Best,
> Paul
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
Rui Barradas
2022-May-09 09:22 UTC
[R] Filtering an Entire Dataset based on Several Conditions
Hello, Something like this? First normalize the data. Then a apply loop creates a logical matrix giving which numbers are in the range -3 to 3. If they are all TRUE then their sum by rows is equal to the number of columns. This creates a logical index i. Use that index i to subset the scaled data set. # test data set, remove the Species column (not numeric) df1 <- iris[-5] df1_norm <- scale(df1) i <- rowSums(apply(df1_norm, 2, \(x) x > -3 & x < 3)) == ncol(df1_norm) # returns a matrix df1_norm[i, ] # returns a data.frame as.data.frame(df1_norm[i,]) Hope this helps, Rui Barradas ?s 09:23 de 09/05/2022, Paul Bernal escreveu:> Dear friends, > > I have a dataframe which every single (i,j) entry (i standing for ith row, > j for jth column) has been normalized (converted to z-scores). > > Now I want to filter or subset the dataframe so that I only end up with a a > dataframe containing only entries greater than -3 or less than 3. > > How could I accomplish this? > > Best, > Paul > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.