View dataframes by pasting its name in r

拈花ヽ惹草 提交于 2019-12-11 12:47:05

问题


Is there any way to View dataframes in r, while refering to them with another variable? Say I have 10 data frames named df1 to df10, is there a way I can View them while using i instead of 1:10? Example:

df1 = as.data.frame(c(1:20))
i = 1

View(paste("df", i, sep =""))

I would like this last piece of code to do the same as View(df1). Is there any command or similar in R that allows you to do that?


回答1:


The answer to your immediate question is get:

df1 <- data.frame(x = 1:5)
df2 <- data.frame(x = 6:10)
> get(paste0("df",1))
  x
1 1
2 2
3 3
4 4
5 5

But having multiple similar objects with names like df1, df2, etc in your workspace is considered fairly bad practice in R, and instead experienced R folks will prefer to put related objects in a named list:

df_list <- setNames(list(df1,df2),paste0("df",1:2))
> df_list[[paste0("df",1)]]
  x
1 1
2 2
3 3
4 4
5 5


来源:https://stackoverflow.com/questions/36336463/view-dataframes-by-pasting-its-name-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!