基于阈值筛选达标频率字符及提取df中高频Factory名称至向量
Got it, let's break this down step by step. You want to pull all Factory names where the freq value is over 15 from your data frame df and store them in a vector. Here are a couple of straightforward ways to do this in R:
First, Let's Recreate Your Data Frame
First, let's make sure we're working with the exact data you provided. Here's how to build the data frame:
df <- data.frame( Factory = c( "F63F5C2CC9ADEC78", "437D11819C8F3086", "BCCFA6F2C54A964B", "0C1DFC7996E98A98", "4DBE085C274FC0D2", "A8FCA1AD604D3A61", "B33691F8279D733C", "001DD6C2202E54F1", "BBBC5737EFE9C6F5", "09FDC29D7442958A", "4A61DE171F2743E7", "62131A16C832AB49", "73DF23BF482EE5FE", "793C792AE6E71D33", "5F3A38C49F3C3296", "923963E76AF1360D", "D7055DCB51E1297A", "1F4D81F7A9BC7031", "898C2388F2312392", "CAD1A7D01E482069" ), freq = c(93,73,72,60,32,31,28,25,25,21,19,18,18,16,6,6,6,4,2,2) )
Method 1: Base R (No Extra Packages Needed)
This is the simplest approach if you don't want to load additional libraries:
# Filter rows where freq > 15, then extract the Factory column into a vector high_freq_factories <- df$Factory[df$freq > 15]
How this works:
df$freq > 15creates a logical vector (TRUE/FALSE) marking which rows meet the threshold.- We use this logical vector to index into
df$Factory, pulling only the names where the condition is TRUE.
Method 2: Using dplyr (Tidyverse Style)
If you prefer a more readable, pipe-based workflow, use the dplyr package (install it first if you haven't already):
# Install dplyr if you haven't (run once) # install.packages("dplyr") library(dplyr) # Filter and extract in one chain high_freq_factories <- df %>% filter(freq > 15) %>% # Keep only rows where freq exceeds 15 pull(Factory) # Extract the Factory column as a standalone vector
Check the Result
To confirm you've got the right values, just print the vector:
print(high_freq_factories)
You'll see it contains the first 14 Factory names from your data frame (all entries with freq from 16 up to 93).
内容的提问来源于stack exchange,提问作者Asha

