简体   繁体   English

如何从csv中读取数据帧名称并在R中的循环中使用它

[英]How to read data frame name from a csv and use it in a loop in R

I have created some 500 data frames in the global environment of R studio and I have the list of all these data frame names in a csv file. 我已经在R Studio的全局环境中创建了大约500个数据框,并且在csv文件中列出了所有这些数据框名称。 I need to collect the no. 我需要收集没有。 of observations of each data frames. 每个数据帧的观测值。 I know I need to use the nrow(dataset_name) command but is there any way, I can create a loop such that R reads the dataset names from the csv and executes the nrow command? 我知道我需要使用nrow(dataset_name)命令,但是有什么办法,我可以创建一个循环,使R从csv读取数据集名称并执行nrow命令?

ps- I am a newbie to R, so plz pardon me if this question is very basic. ps-我是R的新手,所以请问这个问题是否很基本。 TIA! TIA!

Regards, Brock 问候,布洛克

Sample Example Below: 下面的示例示例:

 # Creating 3 data frames
 df1=data.frame(a=1:12,b=1:12)   # Number of rows 12
 df2=data.frame(a=1:5,b=1:5)     # Number of rows 5
 df3=data.frame(a=1:20,b=1:20)   # Number of rows 20

 # Storing the created data frames in a list
 df_names=list(df1,df2,df3)

 # Computing the number of rows for each data frame
 for(i in 1:length(df_names)){
 print(nrow(df_names[[i]]))
 }
 # [1] 12
 # [1] 5
 # [1] 20

Note that here the data frames created are all saved in a list, not the dataframe names but the data frames and not in the csv file. 请注意,此处创建的数据帧都保存在列表中,而不是数据帧名称中,而是数据帧中,而不是csv文件中。

If you have 500 dataframes with you with data frames name as df1,df2,....,df499, df500, you can create list using paste and then you can apply the same above for loop. 如果您有500个数据框,且数据框名称分别为df1,df2,...,df499,df500,则可以使用paste创建列表,然后可以将上面的内容应用于循环。

df_names=list(paste("df",1:500,sep=""))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM