1

dfs is a list whose element is a data frame containing information of environment in a city. And I want to get PM_US.Post of each city in one column.

I've tried: PM_2.5 <- sapply(dfs,[[,"PM_US.Post")

What I've got is each column represents one city's PM_US.Post: enter image description here

But I want to get them in one column.

smci
  • 32,567
  • 20
  • 113
  • 146
Chengxuan
  • 11
  • 1

1 Answers1

1

You could do

do.call(rbind, lapply(dfs, `[`, 'PM_US.Post'))

Or with purrr

purrr::map_df(list_df, `[`, 'PM_US.Post')

Using a reproducible example

df1 <- data.frame(a = 1:5, b = 6:10)
df2 <- data.frame(a = 11:15, c = 16:20)
dfs <- list(df1, df2)

do.call(rbind, lapply(dfs, `[`, 'a'))
#    a
#1   1
#2   2
#3   3
#4   4
#5   5
#6  11
#7  12
#8  13
#9  14
#10 15
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213