R 如何将具有相同引用的一组行中的特定行#与包含具有匹配引用的单行的df合并#
我对堆栈溢出还是个新手,所以请告诉我是否有更好的方法在我的问题中包含数据或其他格式问题。谢谢 我有两个数据帧。其中一个包含我需要的一行数据,该数据具有唯一的引用号 我需要将第一个数据帧中的Ph和solved02合并到具有纬度和经度的数据帧中。但我只想计算每个唯一参考号最后一行的值,或者换句话说,最深的pH值和溶解02值。最终数据帧中每个参考号只出现一次。可以使用以下代码创建每个数据帧的示例(可能是将数据输入堆栈溢出的更简单的方法?)R 如何将具有相同引用的一组行中的特定行#与包含具有匹配引用的单行的df合并#,r,dataframe,merge,R,Dataframe,Merge,我对堆栈溢出还是个新手,所以请告诉我是否有更好的方法在我的问题中包含数据或其他格式问题。谢谢 我有两个数据帧。其中一个包含我需要的一行数据,该数据具有唯一的引用号 我需要将第一个数据帧中的Ph和solved02合并到具有纬度和经度的数据帧中。但我只想计算每个唯一参考号最后一行的值,或者换句话说,最深的pH值和溶解02值。最终数据帧中每个参考号只出现一次。可以使用以下代码创建每个数据帧的示例(可能是将数据输入堆栈溢出的更简单的方法?) sample.df下面使用dplyr的groupby\u和su
sample.df下面使用dplyr
的groupby\u和summary获得引用出现的最后一行,然后针对每个引用过滤最后一行上的DF1,最后合并DF2中的所有列
library(dplyr)
df$id <- c(1:nrow(df)) # Create ID Column to store row number
# Create a smaller df with just the references and the max row number (which should equal the last occurance)
df1_last_references <- df %>%
group_by(Reference) %>%
summarise(id = max(id))
# Filter Original DF on the row numbers matching from the last references
df <- df[which(df$id %in% df1_last_references$id), ]
# merge in the columns from DF2
df3 <- merge(df, df2, all.x = T, by = 'Reference')
head(df3)
Reference Gear Longitude Latitude StartDepth Zone pH Dissolved02
1 BBM2008050101 301 -83.44165 29.637633 1.6 D 8.2 6.1
2 BBM2008050102 301 -83.439717 29.630233 1.8 D 8.1 5.9
3 BBM2008050103 301 -83.434017 29.605567 1.8 D 8.2 6.1
4 BBM2008050104 301 -83.440067 29.596267 1.8 D 8.2 6.5
5 BBM2008050105 301 -83.4346 29.592667 1.2 D 8.2 7.9
6 BBM2008050106 300 -83.44555 29.596917 2.5 D 8.3 6.4
库(dplyr)
df$id%
总结(id=最大(id))
#根据与上次引用匹配的行号筛选原始DF
df使用数据的选项。表
:
DT2[, c("pH", "Dissolved02") :=
DT1[.SD, on=.(Reference), mult="last", .(pH, DissolvedO2)]
]
输出(DT2
):
数据:
库(data.table)
DT1如果您正在寻找“将数据输入堆栈溢出的更简单方法”,我建议发布dput(your.data.frame)
的输出。输出的形式可以复制/粘贴到终端,以复制源数据帧。如果您想发送数据的快照,可以使用head(your.data.frame,num.first.rows)
,或者(如果您是芭蕾运动员),使用sample()
,对数据进行采样,因此使用:dput(your.df[sample[nrow(your.df),num.rows),]
。了解非常有用的信息。谢谢。您如何学习所有这些简单的小函数,如dput()当有这么多的时候?使用dput
和一些其他技术在相当流行的帖子中都提到了,比如和。我不知道dput
,直到我开始四处查看并在so中发布。看起来很有用!你可能已经熟悉head()
,但我发现使用df[sample(…),]
如果你想从一个更大的数据集中随机抽取一个样本,只抽取前20行并不代表多个因素,等等,那就更好了。我同意,有时候当所有因素都被分组时,如果你只取head(),你可能会得到一个非常相似的样本。我尝试了物理[,c(“pH”,“solved02”):=rawhog[.SD,on=(参考),mult=“last”,(pH,溶解氧)]]
但什么也没发生。没有合并数据表。不会有新的data.table。它会通过引用更新“物理”data.table。我尝试切换数据帧,但无论哪种方式,我的数据帧仍然具有相同的变量,没有添加新的变量。是否有错误消息?您需要使用s将2 data.frames转换为data.tablesETDT没有错误,它看起来运行正常,它们是数据表,只是没有任何更改。奇怪。另一种方法对我有效。谢谢!
library(dplyr)
df$id <- c(1:nrow(df)) # Create ID Column to store row number
# Create a smaller df with just the references and the max row number (which should equal the last occurance)
df1_last_references <- df %>%
group_by(Reference) %>%
summarise(id = max(id))
# Filter Original DF on the row numbers matching from the last references
df <- df[which(df$id %in% df1_last_references$id), ]
# merge in the columns from DF2
df3 <- merge(df, df2, all.x = T, by = 'Reference')
head(df3)
Reference Gear Longitude Latitude StartDepth Zone pH Dissolved02
1 BBM2008050101 301 -83.44165 29.637633 1.6 D 8.2 6.1
2 BBM2008050102 301 -83.439717 29.630233 1.8 D 8.1 5.9
3 BBM2008050103 301 -83.434017 29.605567 1.8 D 8.2 6.1
4 BBM2008050104 301 -83.440067 29.596267 1.8 D 8.2 6.5
5 BBM2008050105 301 -83.4346 29.592667 1.2 D 8.2 7.9
6 BBM2008050106 300 -83.44555 29.596917 2.5 D 8.3 6.4
DT2[, c("pH", "Dissolved02") :=
DT1[.SD, on=.(Reference), mult="last", .(pH, DissolvedO2)]
]
Reference Gear Longitude Latitude StartDepth Zone pH Dissolved02
1: BBM2008050101 301 -83.44165 29.63763 1.6 D 8.2 6.1
2: BBM2008050102 301 -83.43972 29.63023 1.8 D 8.1 5.9
3: BBM2008050103 301 -83.43402 29.60557 1.8 D 8.2 6.1
4: BBM2008050104 301 -83.44007 29.59627 1.8 D 8.2 6.5
5: BBM2008050105 301 -83.43460 29.59267 1.2 D 8.2 7.9
6: BBM2008050106 300 -83.44555 29.59692 2.5 D 8.3 6.4
library(data.table)
DT1 <- fread("Reference Depth Beg_end Temperature Conductivity pH Salinity DissolvedO2
BBM2008050101 0.2 B 24.8 52.1 8.2 34.3 6.1
BBM2008050101 1.0 B 24.8 52.4 8.2 34.5 6.1
BBM2008050101 1.4 B 24.8 52.4 8.2 34.5 6.1
BBM2008050102 0.2 B 24.5 53.0 8.1 35.0 6.3
BBM2008050102 1.0 B 24.5 53.0 8.1 34.9 6.0
BBM2008050102 1.6 B 24.5 53.0 8.1 35.0 5.9
BBM2008050103 0.2 B 24.9 51.1 8.2 33.5 6.1
BBM2008050103 1.0 B 24.9 51.1 8.2 33.5 6.1
BBM2008050103 1.6 B 24.9 51.1 8.2 33.5 6.1
BBM2008050104 0.2 B 25.1 51.4 8.2 33.8 6.7
BBM2008050104 1.0 B 25.1 51.4 8.2 33.8 6.5
BBM2008050104 1.6 B 25.1 51.4 8.2 33.8 6.5
BBM2008050105 0.2 B 24.9 51.9 8.1 34.1 7.7
BBM2008050105 1.0 B 24.9 51.9 8.2 34.1 7.9
BBM2008050106 0.2 B 25.4 51.1 8.3 33.5 7.0
BBM2008050106 1.0 B 25.4 51.1 8.3 33.5 6.5
BBM2008050106 2.0 B 25.4 51.1 8.3 33.5 6.5
BBM2008050106 2.3 B 25.4 51.1 8.3 33.5 6.4")
DT2 <- fread("Reference Gear Longitude Latitude StartDepth Zone
BBM2008050101 301 -83.44165 29.637633 1.6 D
BBM2008050102 301 -83.439717 29.630233 1.8 D
BBM2008050103 301 -83.434017 29.605567 1.8 D
BBM2008050104 301 -83.440067 29.596267 1.8 D
BBM2008050105 301 -83.4346 29.592667 1.2 D
BBM2008050106 300 -83.44555 29.596917 2.5 D")