Apologies, this seems like an easy question but I can't find the answer.
I'm using key word groups to search strings for important phrases. My table (srchtbl) classifies words by category (general thing they refer to) and component (actions vs. descriptions)
My method requires that I drill down to vectors to extract word groups to search. I'm able to create vectors for each category name and each component.
However, I also want to make dataframes for each category that are named by the category.
my data:
word pattern category component
<chr> <chr> <chr> <chr>
1 pack pack pkg action
2 protect protect pkg action
3 well well pkg description
4 clever clever pkg description
5 care care pkg description
6 safe safe pkg description
These statements create the appropriate dataframe with the appropriate name:
catgroups <- unique(srchtbl$category)
assign(paste("df_", catgroups[i], sep = ""), srchtbl %>% filter(category == catgroups[i]) %>% group_by(component))
which is fine, but how do I refer to it without using the whole statement? if I use:
print(paste("df_", catgroups[3], sep = ""))
[1] "df_pkg"
So it's like I can't reference it again without using the entire assign statement.
Is there another way to concatenate a dataframe name and make a simple assignment, like:
"string" + catgroups[i] <- srchtbl %>% filter(category == catgroups[3]) %>% group_by(component))
Ultimately the code will be looped so that the key word table can expand to any number of categories and components, so I don't want to type individual dataframe names
Consider base R's by
or split
which creates a named list of data frames from one or more grouping(s) where you can reference the individual data frames with $
or [[
qualifier. No need to flood global environment with many similarly structured objects. Instead maintain one list
object. You lose no functionality of a data frame if stored in a list
.
df_list1 <- split(srchtbl, srchtbl$category)
df_list1$pkg
# word pattern category component
# 1 pack pack pkg action
# 2 protect protect pkg action
# 3 well well pkg description
# 4 clever clever pkg description
# 5 care care pkg description
# 6 safe safe pkg description
dflist2 <- by(srchtbl, srchtbl$category, identity)
dflist2[['pkg']]
# word pattern category component
# 1 pack pack pkg action
# 2 protect protect pkg action
# 3 well well pkg description
# 4 clever clever pkg description
# 5 care care pkg description
# 6 safe safe pkg description
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.