Using this code, I made the first row of the mtcars example as "p-values"
I want to make a conditional code where within that first row, if the p-value is 0.05 or less, keep it, and anything larger, that specific column is dropped.
DF<- as.data.frame(mtcars)
rownames(DF)[1] <- "p-value"
DF_row <- rownames(DF)
DF <- cbind(DF_row, DF)
rownames(DF) <- NULL
p_val <- c(0.01, 0.2, 0.1, 0.03, 0.014, 0.09, 0.05, 0.01, 0.03, 0.04, 0.011)
colnames(DF)[1] <- "Cars"
DF[2:12] <- p_val
What would be the best way in doing so? I looked everywhere online, but I can't seem to find the best way that works for this. Any suggestion is much appreciated
CodePudding user response:
You can check only the first row then bind all the other rows again.
Here is an example with Dplyr.
Solution
I'm identifying the p>.05 by putting 9999 in place, everything else is the same value.
library(dplyr)
DF[1,] %>% mutate(across(is.numeric, ~ifelse(.x < .05, .x, 9999))) %>%
bind_rows(DF[-1,])
alternatively, you can identify them as missing putting NA_real_
DF[1,] %>% mutate(across(is.numeric, ~ifelse(.x < .05, .x, NA_real_ ))) %>%
bind_rows(DF[-1,])
Output
Cars mpg cyl disp hp drat wt qsec vs am gear carb
1 p-value 0.010 0.011 0.040 0.030 0.010 9999.000 9999.000 0.014 0.030 9999.000 9999.000
2 Mazda RX4 Wag 0.200 0.010 0.011 0.040 0.030 0.010 0.050 0.090 0.014 0.030 0.100
3 Datsun 710 0.100 0.200 0.010 0.011 0.040 0.030 0.010 0.050 0.090 0.014 0.030
4 Hornet 4 Drive 0.030 0.100 0.200 0.010 0.011 0.040 0.030 0.010 0.050 0.090 0.014
5 Hornet Sportabout 0.014 0.030 0.100 0.200 0.010 0.011 0.040 0.030 0.010 0.050 0.090
6 Valiant 0.090 0.014 0.030 0.100 0.200 0.010 0.011 0.040 0.030 0.010 0.050
7 Duster 360 0.050 0.090 0.014 0.030 0.100 0.200 0.010 0.011 0.040 0.030 0.010
8 Merc 240D 0.010 0.050 0.090 0.014 0.030 0.100 0.200 0.010 0.011 0.040 0.030
9 Merc 230 0.030 0.010 0.050 0.090 0.014 0.030 0.100 0.200 0.010 0.011 0.040
10 Merc 280 0.040 0.030 0.010 0.050 0.090 0.014 0.030 0.100 0.200 0.010 0.011
11 Merc 280C 0.011 0.040 0.030 0.010 0.050 0.090 0.014 0.030 0.100 0.200 0.010
12 Merc 450SE 0.010 0.011 0.040 0.030 0.010 0.050 0.090 0.014 0.030 0.100 0.200
13 Merc 450SL 0.200 0.010 0.011 0.040 0.030 0.010 0.050 0.090 0.014 0.030 0.100
14 Merc 450SLC 0.100 0.200 0.010 0.011 0.040 0.030 0.010 0.050 0.090 0.014 0.030
15 Cadillac Fleetwood 0.030 0.100 0.200 0.010 0.011 0.040 0.030 0.010 0.050 0.090 0.014
16 Lincoln Continental 0.014 0.030 0.100 0.200 0.010 0.011 0.040 0.030 0.010 0.050 0.090
17 Chrysler Imperial 0.090 0.014 0.030 0.100 0.200 0.010 0.011 0.040 0.030 0.010 0.050
18 Fiat 128 0.050 0.090 0.014 0.030 0.100 0.200 0.010 0.011 0.040 0.030 0.010
19 Honda Civic 0.010 0.050 0.090 0.014 0.030 0.100 0.200 0.010 0.011 0.040 0.030
20 Toyota Corolla 0.030 0.010 0.050 0.090 0.014 0.030 0.100 0.200 0.010 0.011 0.040
21 Toyota Corona 0.040 0.030 0.010 0.050 0.090 0.014 0.030 0.100 0.200 0.010 0.011
22 Dodge Challenger 0.011 0.040 0.030 0.010 0.050 0.090 0.014 0.030 0.100 0.200 0.010
23 AMC Javelin 0.010 0.011 0.040 0.030 0.010 0.050 0.090 0.014 0.030 0.100 0.200
24 Camaro Z28 0.200 0.010 0.011 0.040 0.030 0.010 0.050 0.090 0.014 0.030 0.100
25 Pontiac Firebird 0.100 0.200 0.010 0.011 0.040 0.030 0.010 0.050 0.090 0.014 0.030
26 Fiat X1-9 0.030 0.100 0.200 0.010 0.011 0.040 0.030 0.010 0.050 0.090 0.014
27 Porsche 914-2 0.014 0.030 0.100 0.200 0.010 0.011 0.040 0.030 0.010 0.050 0.090
28 Lotus Europa 0.090 0.014 0.030 0.100 0.200 0.010 0.011 0.040 0.030 0.010 0.050
29 Ford Pantera L 0.050 0.090 0.014 0.030 0.100 0.200 0.010 0.011 0.040 0.030 0.010
30 Ferrari Dino 0.010 0.050 0.090 0.014 0.030 0.100 0.200 0.010 0.011 0.040 0.030
31 Maserati Bora 0.030 0.010 0.050 0.090 0.014 0.030 0.100 0.200 0.010 0.011 0.040
32 Volvo 142E 0.040 0.030 0.010 0.050 0.090 0.014 0.030 0.100 0.200 0.010 0.011