2015-12-15 25 views
0

我有一个包含数百个CSV文件的文件夹,每个CSV文件都包含特定邮政编码的数据。R - 将多个CSV中的行组合到一个数据框中

每个CSV文件包含两列和数千行。描述符在列A中,值在列B中。

我需要从每个文件中提取两条信息,并使用[列A,行2]中的值(它是邮政信息)创建新表或数据框代码)和[B栏第1585行](这是收入中位数)。

最终结果应该是包含两列的表/数据框:一个用于邮政编码,另一个用于中间收入。

任何帮助或建议,将不胜感激。

+0

传递'rbind.data.frame'到'do.call'内的'read.table/read.csv'来获取目录中的所有'csv'文件。它可以在一行中完成。 – Frash

+0

Further reading http://www.r-bloggers.com/the-rbinding-race-for-vs-do-call-vs-rbind-fill/ – Frash

回答

0

免责声明:这个问题是相当模糊的。下一次,一定要添加一个我们可以在我们的机器上运行的可重复的例子。它会帮助你,回答你的问题的人和未来的用户。

你可以尝试这样的:

files = list.files("~/Directory") 

my_df = data.frame(matrix(ncol = 2, nrow = length(files) 

for(i in 1:length(files)){ 
    row1 = read.csv("~/Directory/files[i]",nrows = 1) 
    row2 = read.csv("~/Directory/files[i]", skip = 1585, nrows = 1) 
    my_df = rbind(my_df, rbind(row1, row2)) 
} 

my_df = my_df[,c("A","B")] 
# Note on interpreting indexing syntax: 
    Read this as "my_df is now (=) my_df such that ([) the columns (,) 
    are only A and B (c("A", "B")) " 
+0

你可能意思是'rbind(row1,row2)' – Frash

+0

@ Frash我的意思是两者(尽管这并不排除错误的可能性);我想将现有的数据框(我正在构建的)与新行绑定(也需要绑定在一起)。 – Nancy

+0

问题是行1只有一条信息,而行1585有两条。你的代码和建议给了我很多工作,所以我应该顺利。 – Pubb

0

可以使用list.files函数来获取目录中的所有文件,然后使用read.csvrbindfor循环创建一个data.frame

事情是这样的:

direct<-list.files("directory_to_your_files") 
df<-NULL 
for(i in length(direct)){ 
    df<-rbind(df,read.csv(direct[i])) 
} 
0

因此,这里的代码,做什么,我想要它做的。如果有更优雅的解决方案,请随时指出。

# set the working directory to where the data files are stored 
setwd("/foo") 

# count the files 
files = list.files("/foo") 

#create an empty dataframe and name the columns 

dataMatrix=data.frame(matrix(c(rep(NA,times=2*length(files))),nrow=length(files))) 
colnames(dataMatrix)=c("Postal Code", "Median Income") 

# create a for loop to get the information in R2/C1 and R1585/C2 of each data file 
# Data is R2/C1 is a string, but is interpreted as a number unless specifically declared a string 

for(i in 1:length(files)) { 
    getData = read.csv(files[i],header=F) 
    dataMatrix[i,1]=toString(getData[2,1]) 
    dataMatrix[i,2]=(getData[1585,2]) 
} 

谢谢所有帮助我解决这个问题的人,特别是Nancy。

相关问题