如何在 R 列表中的多个数据帧中访问相同的变量?

发布于 2025-01-20 08:36:30 字数 476 浏览 0 评论 0原文

我正在开发一个新项目,其中有一个 tibbles/dataframes 列表。每个数据框是程序的每周结果,并且数据框每周具有相同的变量(例如日期、时间、参与者、主题),但每周的行数不同。我希望能够在不使用循环的情况下访问所有数据帧中一个变量的所有条目。例如,现在,如果我想要所有名称,我有:

all_emails<-Fall2021[[1]]$email
for (i in 2:length(Fall2021)) {
  all_emails<-c(all_signups,Fall2021[[i]]$email)
}

是否有一种更简单的方法可以在不使用循环的情况下完成此操作。我知道我可以通过以下方式访问单个数据框中的单个变量:

Fall2021[[1]]$email

但是有没有像这样的东西实际上有效:

Fall2021[[:]]$email

I'm working on a new project where I'm have a list of tibbles/dataframes. Each dataframe is the weekly outcome of a program, and the dataframes have the same variables each week (e.g. date, time, participants, topic), but varying numbers of rows each week. I would like to be able to access all entries of one variable across all the dataframes without using a loop. For instance, right now, if I want all the names, I have:

all_emails<-Fall2021[[1]]$email
for (i in 2:length(Fall2021)) {
  all_emails<-c(all_signups,Fall2021[[i]]$email)
}

Is there an easier way to do this without a loop. I know I can access a single variable in a single data frame with:

Fall2021[[1]]$email

But is there something like this, that actually works:

Fall2021[[:]]$email

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌面超妹 2025-01-27 08:36:30

这是你想要的吗?

library(dplyr)
library(purrr)
lst <- list(
df1 <- tibble(date = c(2000:2009), 
              time = c(1:10),
              participants = c(30:39),
              topic = c(10:19)),

df2 <- tibble(date = c(2000:2020), 
              time = c(1:21),
              participants = c(30:50),
              topic = c(10:30))


)

lst %>% map_dfr(~ .x %>% select(date))

Does this what you want?

library(dplyr)
library(purrr)
lst <- list(
df1 <- tibble(date = c(2000:2009), 
              time = c(1:10),
              participants = c(30:39),
              topic = c(10:19)),

df2 <- tibble(date = c(2000:2020), 
              time = c(1:21),
              participants = c(30:50),
              topic = c(10:30))


)

lst %>% map_dfr(~ .x %>% select(date))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文