我知道有几种从数据框/小标题中获取单个值的方法。
library(dplyr)
start_date <- tibble::tribble(
~StaffAbbrev, ~starting_date,
"Alexander", "2021-08-23",
"Cornelis", "2021-08-23",
"Sotirchos", "2021-08-23",
"Zhao", "2021-08-23",
"Park", "2022-02-14",
"Sarkar", "2022-04-04"
)
#Tidyverse way
Alexander_start_v1 <- start_date %>%
filter(StaffAbbrev == "Alexander") %>%
select(starting_date) %>%
unlist() %>%
unname()
#Base R way
Alexander_start_v2=start_date$starting_date[start_date$StaffAbbrev=="Alexander"]
Run Code Online (Sandbox Code Playgroud)
是速记/更优雅/单行的 tidyverse 方式从数据帧/小标题中提取单个特定值吗?
以下是一些可能性:
library(dplyr)
library(tibble)
start_date %>% deframe %>% getElement("Alexander")
## [1] "2021-08-23"
library(dplyr)
library(tibble)
start_date %>% deframe %>% .[["Alexander"]]
## [1] "2021-08-23"
library(dplyr)
start_date %>% filter(StaffAbbrev == "Alexander") %>% pull
## [1] "2021-08-23"
library(dplyr)
library(magrittr)
start_date %>% filter(StaffAbbrev == "Alexander") %$% starting_date
## [1] "2021-08-23"
Run Code Online (Sandbox Code Playgroud)
这是基本的 R 代码
with(start_date, starting_date[match("Alexander", StaffAbbrev)])
## [1] "2021-08-23"
Run Code Online (Sandbox Code Playgroud)