英文:
how to use Purrr/reduce combine dataframe objects
问题
在R环境中有名为'data_1\data_2\....data_20'
的data.frame对象,
reduce(objects, rbind)
失败了,有人可以帮忙吗?谢谢!
library(tidyverse)
for (i in c(1:20)){
assign(paste0('data_', i), data.frame(category = c('a', 'b'), sales = c(1, 2)))
}
objects <- ls(pattern = 'data') %>% as.list()
data_total <- reduce(objects, rbind)
下面的代码可以工作,但必须将data_1\data_2\....data_20
作为参数输入:
data_total <- reduce(list(data_1, data_2), rbind)
英文:
There are data.frame objects in R environment named as 'data_1\data_2\....data_20'
,
reduce(objects,rbind)
failed ,anyone can help ? Thanks!
library(tidyverse)
for (i in c(1:20)){
assign(paste0('data_',i),data.frame(category=c('a','b'),sales=c(1,2)))
}
objects <- ls(pattern='data') %>% as.list()
data_total <- reduce(objects,rbind)
below code can work,but have to input data_1\data_2\....data_20
as parmeter
data_total <- reduce(list(data_1,data_2),rbind)
答案1
得分: 1
以下是翻译好的部分:
你可以这样做:
library(tidyverse)
data_frames <- str_c("data_", 1:20)
map_dfr(data_frames, get)
请注意,代码部分没有进行翻译。
英文:
You can do this:
library(tidyverse)
data_frames <- str_c("data_", 1:20)
map_dfr(data_frames, get)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论