0

I am working with multiple dataframes in R and I want to access those dataframes with a single command. I used the ls command to capture the dataframes by their name but I am not sure how to access the dataframe itself.

first_df  <- data.frame(y1 <- c(1, 2, 3), y2 <- c(4, 5, 6))
second_df <- data.frame(y1 <- c(7, 8, 9), y2 <- c(10, 11, 12))

ls(pattern = "_df")
print(class(ls(pattern = "_df")[1]))
print(class(ls(pattern = "_df")[[1]]))

# I want to see the 2 rows of the dataset here.
head(ls(pattern = "_df")[[1]])
Hamideh
  • 665
  • 2
  • 8
  • 20
  • `get(ls(pattern = "_df")[1])` or `mget(ls(pattern = "_df"))` – Ronak Shah Apr 29 '21 at 05:18
  • It is not about the command. Your dataframe doesn't have correct column names originally. Did you see the output of `first_df <- data.frame(y1 <- c(1, 2, 3), y2 <- c(4, 5, 6))` ? – Ronak Shah Apr 29 '21 at 06:05

0 Answers0