#r
Вопрос:
У меня есть фрейм данных df1. Как удалить те,у которых символ меньше 10 (печать, ошибка и нет) из каждого столбца?
df1 >
2019 2020 2021
<chr> <chr> <chr>
1 Print The value of x is 6 The value of x is 1
2 The value of x is 2 The value of y is 6 The value of y is 0
3 NA error The value of z is 9
4 NA NA no
dput(d1) >
structure(list(`2019` = c("Print", "The value of x is 2", NA,
NA), `2020` = c("The value of x is 6", "The value of y is 6",
"error", NA), `2021` = c("The value of x is 1", "The value of y is 0",
"The value of z is 9", "no")), row.names = c(NA, -4L), class = c("tbl_df",
"tbl", "data.frame"))
Результатом моего желания является
2019 2020 2021
<chr> <chr> <chr>
1 The value of x is 2 The value of x is 6 The value of x is 1
2 NA The value of y is 6 The value of y is 0
3 NA NA The value of z is 9
введите код здесь
Ответ №1:
> list2DF(lapply(d1, function(x) x[nchar(x) >= 10]))
2019 2020 2021
1 The value of x is 2 The value of x is 6 The value of x is 1
2 <NA> The value of y is 6 The value of y is 0
3 <NA> <NA> The value of z is 9
Ответ №2:
Используйте across
с summarise
:
library(dplyr)
df1 %>% summarise(across(.fns = ~.x[nchar(.x) >= 10]))
# `2019` `2020` `2021`
# <chr> <chr> <chr>
#1 The value of x is 2 The value of x is 6 The value of x is 1
#2 NA The value of y is 6 The value of y is 0
#3 NA NA The value of z is 9
В базе R :
data.frame(sapply(df1, function(x) x[nchar(x) >= 10]))