问题描述
我有以下问题:在一个csv文件中,我有一列用于物种,一列用于横断面,一列用于年份,一列用于AUC.在另一个csv文件中,我有一列用于横断面,一列用于年份,一列用于降水,一列用于温度.现在,我想以某种方式加入R中的文件,这样我就可以从第二个csv获得种类和AUC的列,而从第一个csv获得其余的列.最后,我想获取一个包含transect_id,年,日,月,种类,regional_gam(= AUC),降水和LST(=温度)的文件.因此,基本上,必须将2008年每天从TR001开始的降水/LST值分配给每个具有2008年AUC值和TR001的物种.谢谢!
I have the following problem: in a csv-file I have a column for species, one for transect, one for the year and one for the AUC. In another csv-file I have a column for transect, one for the year, one for precipitation and one for temperature. Now I would like to join the files in R in a way, that I can have the columns for species and AUC from the second csv and the columns for the rest from the first csv.In the end I'd like to get a file with transect_id, year, day, month, species, regional_gam(=AUC), precipitation and LST(=temperature).So basically the precipitation-/ LST-values from TR001 for every day in 2008 need to be assigned to every species which has an AUC-value for 2008 and TR001.Thanks!
推荐答案
使用read.csv
,然后使用merge
.
将两个csv文件加载到R中.(不要忘记确保它们的公用变量具有相同的名称!).
Load the two csv files into R. (Don't forget to make sure their common variables share the same name!).
df1<-read.csv(dat1,head=T)
df2<-read.csv(dat2,head=T)
通过它们的共享变量将数据框合并在一起,并添加参数all.x = T(默认值),以确保所有行都保留在包含物种的数据库中.
Merge the dataframes together by their shared variables and add argument all.x=T (the default) to ensure all rows are kept from your database containing species.
merge(df1,df2,by=c('transect_id','year'),all.x=T)
要查看使用测试数据的实际效果,请执行以下操作:
To see this in action using test data:
test<-data.frame(sp=c(rep(letters[1:10],2)),t=c(rep(1:3,2,20)),y=c(rep(2000:2008,len=20)),AUC=1:20)
test2<-data.frame(t=c(rep(1:3,2,9)),y=c(rep(2000:2008,len=9)),ppt=c(1:9),temp=c(11:19))
merge(test,test2,by=c('t','y'),all.x=T)
这篇关于如何从R中2个不同的csv文件中合并数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!