I am looking for a way to use the values in the first row to help filter values I want. Say if I want to keep certain columns in R based on the values in the first row. So in the first row, we have -0.5, 0.7, 1.1, and -1.2.
I want to only keep values that are equal to or greater than 1, or less than or equal to -1.2. Everything else will just be dropped.
So say my original data I have is DF1
| ID | Location | XPL | SNA | AAS | APA |
|---|---|---|---|---|---|
| First | Park | -0.5 | 0.7 | 1.1 | -1.2 |
| Second | School | 2 | 5 | 2 | 3 |
| Second | Home | 4 | 5 | 6 | 4 |
| Third | Car | 1 | 8 | 8 | 5 |
| Third | Lake | 7 | 5 | 4 | 6 |
| Fourth | Prison | 4 | 5 | 1 | 7 |
With the filter, I would now have a new DF:
| ID | Location | AAS | APA |
|---|---|---|---|
| First | Park | 1.1 | -1.2 |
| Second | School | 2 | 3 |
| Second | Home | 6 | 4 |
| Third | Car | 8 | 5 |
| Third | Lake | 4 | 6 |
| Fourth | Prison | 1 | 7 |
What would be the best way for this. I feel there is a way to sort columns based on values from a row, but I am unable to think of the way we can with certain commands.
ID <- c("First", "Second", "Second", "Third", "Third", "Fourth")
Location <- c("Park", "School", "Home", "Car", "Lake", "Prison")
XPL <- c(-0.5,2,4,1,7,4)
SNA <- c(0.7,5,5,8,5,5)
AAS <- c(1.1,2,6,8,4,1)
APA <- c(-1.2,3,4,5,6,7)
DF1 <- data.frame(ID, Location, XPL, SNA, AAS,APA)
CodePudding user response:
If you are using the first row as the basis, you can convert it to a normal integer vector and use the which function to know the indexes that will be kept.
test.row <- as.numeric(DF1[1,3:6])
the 3 and 6 corresponds to the range of index from XPL to APA.
DF1 <- DF1[,c(1:2, 2 which(test.row >= 1 | test.row <= -1.2))]
we keep the columns ID and Location as 1:2 and we offset 2 to the which function.
CodePudding user response:
In dplyr, you can select numeric columns whose first absolute value is above 1:
library(dplyr)
DF1 %>%
select(!where(~ is.numeric(.x) && abs(first(.x)) <= 1))
# ID Location AAS APA GOP
# 1 First Park 1.1 -1.2 1.4
# 2 Second School 2.0 3.0 1.0
# 3 Second Home 6.0 4.0 2.0
# 4 Third Car 8.0 5.0 2.0
# 5 Third Lake 4.0 6.0 3.0
# 6 Fourth Prison 1.0 7.0 3.0
Or with between:
DF1 %>%
select(!where(~ is.numeric(.x) && between(first(.x), -1.19, 0.99)))
