我是使用R的新手,我在合并或连接两个数据集时遇到问题。第一个数据集有47列和143,473,数据集2有34和2243行。
第一个数据集由捕捞运输组成,其中包含许多与该运输相关的变量。有一个名为link3的列,它是钓鱼之旅的一个值。
第二个数据集由生物数据组成,其中每行是密封的单个实例。有一个名为link3的列,它是钓鱼之旅的一个值。可以有多个行具有相同的link3。我需要为9种密封添加柱子(每种物种1列),然后总结每种物种的每个物种的采集数量。我成功地做到了这一部分。
我遇到一个问题,即合并或加入将物种密封加到第一个数据集中的结果。我需要将我为第二个数据集中的每个海豹物种创建的列添加到第一个数据集中的所有运输中,但保持第二个数据集中的所有信息不变。我相信这可以称为完全加入,我可以通过谷歌搜索和查看r-blogger,这里等网站收集。
以下是我收到的错误示例:
灰色哈勃竖琴罩密封 933 912 219 4 175 0合并gillnet数据集并密封数据集来自Josh&金佰利
从代码更改为密封件名称
库(MASS) 库(readr) 库(dplyr) 库(plyr) 库(data.table)
setwd(' C:/用户/布施/文档/ NEFSC_Work / SealGear / DataAnalysisFor1990-2014&#39)
读入gillnet数据集
刺网< -read.csv(' C:/Users/dana/Documents/NEFSC_Work/SealGear/Data/gn9014_ForDana2.csv',标题= TRUE) 刺网[1:1,] LINK3年度季节PORTNAME STATE LONDD LONDD_SOURCE 1 000199001C030000001 1990 1 W N.OfBoston 24 -70.055 haul_begin_loc LONDD_SOURCE_CV LATDD LATDD_SOURCE LATDD_SOURCE_CV DEPTH 1 NA 42.44444 haul_begin_loc NA -84.1248 DEPTH_SOURCE DEPTH_SOURCE_CV SLOPE SLOPE_SOURCE SLOPE_SOURCE_CV 1 original_value NA 0.0870441 original_value NA SOAKDUR SOAKDUR_SOURCE SOAKDUR_SOURCE_CV STEAMTIM TIEDNUSD TIEDNUSD_SOURCE 1 48 original_value NA 3 9 original_value PRIMSPP1 ACTMMDUSD MSWGTAVG MSWGTAVG_SOURCE MSWGTAVG_SOURCE_CV TWINESIZ 1 5260 9 5.5 ave_min_max NA 14 GEARLENTOT GEARLENTOT_SOURCE GEARLENTOT_SOURCE_CV QTR TRIPEXT NEGEAR天气 1 NA NA 1 X 100 2 WAVEHGT GEARCOND AREA AREA_SOURCE NNETS NNETS_SOURCE NNETS_SOURCE_CV 1 0 210 514 original_value NA NA MCVERT MCVERT_SOURCE MCVERT_SOURCE_CV HNGRATIO HNGRATIO_SOURCE 1 20 original_value NA 5 original_value HGGRATIO_SOURCE_CV SPACEWID SECMETHOD SEDIMENT REGION2区域PINGPERC 1 NA NA 0 gr-sd GOM GOM 0 FMTONS SECTOR_ID D_COAST D_COAST_SOURCE D_COAST_SOURCE_CV D_B50 1 0.463572 NA 41593.3 original_value NA 13092.4 D_B50_SOURCE D_B50_SOURCE_CV D_B100 D_B100_SOURCE D_B100_SOURCE_CV 1 original_value NA 1902.63 original_value NA D_B200 D_B200_SOURCE D_B200_SOURCE_CV D_B500 D_B500_SOURCE 1 8944.27 original_value NA 273222 original_value D_B500_SOURCE_CV ON_LAND SST SST_SOURCE SST_SOURCE_CV HPTRP 1 NA 0 4.33 ncdcOisst2Agg NA HPTRP_MgmtArea HAUCOMMENTS OBSERVER_ID 1 NEFOP 类(" LINK3&#34) [1]"字符"
读入包含生物信息的密封件数据
海豹< - read.csv(' c:/Users/dana/Documents/NEFSC_Work/SealGear/Data/gn_biodat_9014.csv' ;, header = TRUE) 密封件[1:1,] LINK3 PSID LINK1 NESPP4 ANMLCOND包含ENTANGLE 1 000199006F390000003 1 000199006F39000 6981 11 1 0 NETNUM TIMEBRUP ACTMMDCOND TAGNUM1 TAGCODE1 TAGNUM2 TAGCODE2 ANMLONBD照片 1 00 1130 0 D00192 1 0 1 0 NEGEAR ESTLEN INCCOMMENTS SEX BODYTEMP BLUBTHCK GIRTH TOTLEN FLIPLEN FLIPWID 1 100 NA 1 NA NA 34 NA NA NA 整个JAW STOMACH BLUBBER MUSCLE HEAD RPORGANS BMMCOMMENTS 1 0 1 1 0 0 0 0
sealtypes< - c(' harp' = 6981, +' hood' = 6982, +'环绕' = 6983, +'胡子' = 6984, +' harbs' = 6995, +' ribbon' = 6986, +'拉加' = 6985, +'灰色' = 6996, +' sealnk' = 6994) 海豹$ SPP< - 名称(海豹类)[匹配(密封$ NESPP4,海豹类)]
姓名(刺网)< - tolower(姓名(刺网)) 姓名(印章)< - tolower(姓名(印章)) table(密封$ spp,useNA ='总是')
获取每个物种的摄取次数
密封< - 密封%>% + mutate(harp = ifelse(spp ==' harp',1,0), + hood = ifelse(spp ==' hood',1,0), + ringed = ifelse(spp ==' ringed',1,0), +胡子= ifelse(spp =='胡须',1,0), + harbs = ifelse(spp ==' harbs',1,0), + ribbon = ifelse(spp ==' ribbon',1,0), + larga = ifelse(spp ==' larga',1,0), + gray = ifelse(spp ==' grey',1,0), + sealnk = ifelse(spp ==' sealnk',1,0))
sealum =海豹%>%summarise_each(funs(sum(。,na.rm = T)),harp:sealnk)
sealsum_by_link3< - seal%>%group_by(link3)%>% + summarise_each(funs(sum(。,na.rm = T)),harp:sealnk)%>% + ungroup()
summarise_each是dplyr中
的快捷方式总结(harp = sum(harp,na.rm = T),hood = sum(hood,na.rm = T),...,sealnk = sum(sealnk,na.rm = T)
将创建的表放入数据框
sealum< - as.data.frame.matrix(sealsum_by_link3) sealsum [1:1,] link3竖琴罩环状胡须harbs ribbon larga grey sealnk 1 000199006F390000003 1 0 0 0 0 0 0 0 0 类(" LINK3&#34) [1]"字符"
加入两个数据集
gnobs = gillnet%>%full_join(sealum) 加入:" link3" 错误:无法加入列' link3' x' link3':无法加入' link3' x' link3'因为不兼容的类型(tbl_df,data.frame / factor)
另一次尝试: gnobs = gillnet%>%full_join(sealsum_by_link3) 加入:" link3" 警告信息: 在outer_join_impl(x,y,$ x,$ y)中: 加入不同层次的因素,强制转向角色载体
gnobs [1:1,] link3 year month season portname state londd londd_source 1 000199001C030000001 1990 1 W N.OfBoston 24 -70.055 haul_begin_loc londd_source_cv latdd latdd_source latdd_source_cv深度 1 NA 42.44444 haul_begin_loc NA -84.1248 depth_source depth_source_cv slope slope_source slope_source_cv 1 original_value NA 0.0870441 original_value NA soakdur soakdur_source soakdur_source_cv steamtim tiednusd tiednusd_source 1 48 original_value NA 3 9 original_value primspp1 actmmdusd mswgtavg mswgtavg_source mswgtavg_source_cv twinesiz 1 5260 9 5.5 ave_min_max NA 14 gearlentot gearlentot_source gearlentot_source_cv qtr tripext negear weather 1 NA NA 1 X 100 2 wavehgt gearcond area area_source nnets nnets_source nnets_source_cv 1 0 210 514 original_value NA NA mcvert mcvert_source mcvert_source_cv hngratio hngratio_source 1 20 original_value NA 5 original_value hngratio_source_cv spacewid secmethod the sediment region region2 pingperc 1 NA NA 0 gr-sd GOM GOM 0 fmtons sector_id d_coast d_coast_source d_coast_source_cv d_b50 1 0.463572 NA 41593.3 original_value NA 13092.4 d_b50_source d_b50_source_cv d_b100 d_b100_source d_b100_source_cv 1 original_value NA 1902.63 original_value NA d_b200 d_b200_source d_b200_source_cv d_b500 d_b500_source 1 8944.27 original_value NA 273222 original_value d_b500_source_cv on_land sst sst_source sst_source_cv hptrp 1 NA 0 4.33 ncdcOisst2Agg NA hptrp_mgmtarea haucomments observer_id harp hood ringed beard harbs ribbon 1 NEFOP NA NA NA NA NA NA larga灰色sealnk 1 NA NA NA SUM(gnobs $ harbs) [1] NA
另一次尝试:
gnobs< - merge(x = gillnet,y = sealsum_by_link3,by =" link3",all.x = TRUE)
和(gnobs $ harbs) [1] NA
另一次尝试:
gnobs< - join(gillnet,sealum, + type =" full") 加入方:link3 allocate_column(df [[var]],nrows,dfs,var)出错: 数据框列'竖琴' rbind.fill不支持
尝试了两种类型的合并: gnobs< - merge(gillnet,sealum,by = c(" link3")) #produces正确的数据帧,但它充满了NA
gnobs <- merge(gillnet,sealsum,by="link3",all=TRUE)
#produces this error message:
#Error in if (m < n * p && (m == 0L || (n * p)%%m)) stop(sprintf(ngettext(m, :
#missing value where TRUE/FALSE needed
#In addition: Warning messages:
#1: In `[<-.factor`(`*tmp*`, ri, value = list(link3 = 1L)) :
#invalid factor level, NA generated
#2: In n * p : NAs produced by integer overflow
#3: In n * p : NAs produced by integer overflow
我完全不知道为什么这不起作用。任何人都可以提供任何指导吗?