df1 <- data.frame(a = 1:10, b = 11:20, c= 12:3)
str(df1)
## 'data.frame': 10 obs. of 3 variables:
## $ a: int 1 2 3 4 5 6 7 8 9 10
## $ b: int 11 12 13 14 15 16 17 18 19 20
## $ c: int 12 11 10 9 8 7 6 5 4 3
df1$col <- letters[1:10]
df1
## a b c col
## 1 1 11 12 a
## 2 2 12 11 b
## 3 3 13 10 c
## 4 4 14 9 d
## 5 5 15 8 e
## 6 6 16 7 f
## 7 7 17 6 g
## 8 8 18 5 h
## 9 9 19 4 i
## 10 10 20 3 j
str(df1)
## 'data.frame': 10 obs. of 4 variables:
## $ a : int 1 2 3 4 5 6 7 8 9 10
## $ b : int 11 12 13 14 15 16 17 18 19 20
## $ c : int 12 11 10 9 8 7 6 5 4 3
## $ col: chr "a" "b" "c" "d" ...
df1[,-4] <- sapply(df1[,-4], as.numeric)
##############################
df1[df1[,1:2] > 5,]
## a b c col
## 6 6 16 7 f
## 7 7 17 6 g
## 8 8 18 5 h
## 9 9 19 4 i
## 10 10 20 3 j
## NA NA NA NA <NA>
## NA.1 NA NA NA <NA>
## NA.2 NA NA NA <NA>
## NA.3 NA NA NA <NA>
## NA.4 NA NA NA <NA>
## NA.5 NA NA NA <NA>
## NA.6 NA NA NA <NA>
## NA.7 NA NA NA <NA>
## NA.8 NA NA NA <NA>
## NA.9 NA NA NA <NA>
na.omit(df1[df1[,1:3] > 5,]) ############______does not work
## a b c col
## 6 6 16 7 f
## 7 7 17 6 g
## 8 8 18 5 h
## 9 9 19 4 i
## 10 10 20 3 j
df1[apply(df1[,1:3], 1, function(x) all(x >5, na.rm = TRUE)), ]
## a b c col
## 6 6 16 7 f
## 7 7 17 6 g
###################all
x1 <- c(1, 5, 3, - 3, 5, - 7, 8) # Example vector
x1
## [1] 1 5 3 -3 5 -7 8
all(x1 < 0)
## [1] FALSE
any(x1 < 0) # Apply any function in R
## [1] TRUE
x2 <- c(x1, NA) # Example vector with NA value
x2 # Print vector to RStudio console
## [1] 1 5 3 -3 5 -7 8 NA
# 1 5 3 -3 5 -7 8 NA
all(x2 > - 10) # Apply all function to NA vector
## [1] NA
# NA
any(x2 < - 10) # Apply any function to NA vector
## [1] NA
# NA
all(x2 > - 10, na.rm = TRUE) # Apply all function with na.rm = TRUE
## [1] TRUE
# TRUE
any(x2 < - 10, na.rm = TRUE) # Apply any function with na.rm = TRUE
## [1] FALSE
# FALSE
#ref https://stackoverflow.com/questions/41694759/select-rows-in-a-dataframe-based-on-values-of-all-columns
#https://statisticsglobe.com/all-any-r-function/