【问题标题】:Concatenate two dataframe with condition (coordinates and year corresponding)用条件连接两个数据框(坐标和年份对应)
【发布时间】:2018-09-19 06:00:32
【问题描述】:

我在尝试在特定条件下连接两个数据帧时遇到问题。 我看了不同的帖子,但没有找到对我有帮助的解决方案。

这是我的数据:

Dataframe 1 :

"year"     "var"          "x"                 "y"               "info"
"1992","mean_ndvi","4878686.57157449","5393968.15997648","0.386875003576279"
"1992","mean_ndvi","4896433.83572102","5398120.2484886","0.373374998569489"
"1992","mean_ndvi","4900572.93504345","5370687.20427196","0.394125014543533"
"1992","mean_ndvi","4902934.77310431","5361773.82267221","0.271333336830139"
"1992","mean_ndvi","4763325.11415408","5286260.42907455","0.341958343982697"
"1992","mean_ndvi","4659782.7218849","5251960.76092113","0.407333344221115"
"1992","mean_ndvi","4672416.53746615","5253639.4841048","0.443416655063629"
"1992","mean_ndvi","4688194.71187035","5255824.40292703","0.334916681051254"
"1992","mean_ndvi","4697653.82879809","5257181.46577816","0.367166668176651"

Dataframe 2 :

"year"         "x"             "y"             "species"
 "2014" "4001758.3924046" "3138415.9463486"     "Sus scrofa"
 "2016" "3990684.89200331" "3088575.79671371" "Capreolus capreolus"
 "2014" "4002641.44272945" "3078682.12799716" "Capreolus capreolus"
 "2014" "3946723.09681777" "3153792.59524072" "Capreolus capreolus"
 "2014" "3975356.46700669" "2974349.6604129" "Cervus elaphus"
 "2014" "4001283.9265329" "3137527.57584417" "Capreolus capreolus"
 "2014" "3946723.09681777" "3153792.59524072" "Capreolus capreolus"
 "2014" "3946723.09681777" "3153792.59524072" "Capreolus capreolus"
 "2017" "4000195.01511827" "3103181.07855945" "Capreolus capreolus"

第一个dataframe 包含的data 比第二个多。 我想做的是: concatenate 两个 dataframes 并仅保留第一个 dataframe 中出现的第二个 dataframe 中的 row

我尝试了不同的方法:select and filter, merge, cbind, "by hand" with for loops,但我无法获得任何有效的方法。

我也花了很多时间在网上寻找解决方案,但是,或者我太笨了,看不出如何使用一种解决方案来解决我的问题,或者没有人有同样的问题,我不知道,或者我没有做足够的研究。

如果您对我如何做到这一点有任何线索,我知道这很简单。

Datafrale 1 :

"1992","mean_ndvi","4688194.71187035","5255824.40292703","0.334916681051254"
"1992","mean_ndvi","4697653.82879809","5257181.46577816","0.367166668176651"
"1992","mean_ndvi","4657938.8843526","5242452.09422199","0.43491667509079"
"1992","mean_ndvi","4661111.26475011","5242863.65256642","0.523041665554047"
"1992","mean_ndvi","4692800.91855509","5247191.53424558","0.405791670084"

Dataframe 2 :

"2014" "4001758.3924046" "3138415.9463486" "Sus scrofa"
"2016" "3990684.89200331" "3088575.79671371" "Capreolus capreolus"
"1992" "4657938.8843526" "5242452.09422199" "Capreolus capreolus"
"2017" "4000167.53545378" "3103446.42513062" "Sus scrofa"
"1992" "4688194.71187035" "5255824.40292703 "Capreolus capreolus"

Result : 

"1992" "4657938.8843526" "5242452.09422199" "Capreolus capreolus""0.43491667509079"
"1992" "4688194.71187035" "5255824.40292703 "Capreolus capreolus" "0.334916681051254"

这是 dput 的结果(前 10 行):

First dataframe (with a lot of data)
structure(list(x = c(4878686.57157449, 4896433.83572102, 4900572.93504345, 
4902934.77310431, 4763325.11415408, 4659782.7218849, 4672416.53746615, 
4688194.71187035, 4697653.82879809, 4657938.8843526), y =     c(5393968.15997648, 
5398120.2484886, 5370687.20427196, 5361773.82267221, 5286260.42907455, 
5251960.76092113, 5253639.4841048, 5255824.40292703, 5257181.46577816, 
5242452.09422199), year = c(1993L, 1993L, 1993L, 1993L, 1993L, 
1993L, 1993L, 1993L, 1993L, 1993L), info = c(0.396166652441025, 
0.373374998569489, 0.394125014543533, 0.28979167342186, 0.344375014305115, 
0.414458334445953, 0.416541665792465, 0.342583328485489, 0.378208339214325, 
0.440750002861023)), .Names = c("x", "y", "year", "info"), row.names = c(NA, 
10L), class = "data.frame")

它返回另一个数据帧的整个数据帧,我不明白为什么,但我不能把它说成没有任何意义的结果

【问题讨论】:

  • keep only the row from the first dataframe 是什么意思?
  • 您能否提供具有期望结果的样本数据?就目前而言,您的两个数据框之间没有重叠。
  • 是的对不起,我的意思是“只保留行中的行,其坐标仅出现在第二个数据框中,我将在下面举一个例子
  • 这里,我刚刚编辑了我的帖子,对不起
  • 看看下面的帖子:stackoverflow.com/questions/1299871/….

标签: r dataframe concatenation conditional-statements


【解决方案1】:

试试这个:

# Get fancy data
set.seed(666)

df1 <- iris[sample(x = 1:10, size = 6, replace = FALSE),]
df2 <- iris[sample(x = 1:10, size = 6, replace = FALSE),]

# Get common rows
index <- match(apply(df1, 1, paste, collapse = "-"), 
               apply(df2, 1, paste, collapse = "-"))
index <- index[!is.na(index)]

df3 <- df2[index,]

如您所见,df3 将是一个只有公共行的 data.frame。

【讨论】:

  • 感谢您的回复,但我在我的 24 数据帧上尝试过,同样的问题,它返回 0 个元素。我会看看我的数据的内容,因为它似乎从来没有相同的值,我没有看到其他解释
  • 如果您与我们分享您的一些(有问题的)数据框将会很有用。
  • 我愿意,但是我的数据不会在网络上传播,我该如何与您分享?我使用的文件很大!
  • 不是您的全部数据,只是一些重要的行和列(也许是 10x5?)。使用dput函数。
  • 嗯,你知道我如何使用 dput 函数只给我第一行 10 吗?
猜你喜欢
  • 1970-01-01
  • 2020-11-23
  • 1970-01-01
  • 1970-01-01
  • 2021-12-03
  • 2022-12-07
  • 2016-11-18
  • 2020-09-19
  • 1970-01-01
相关资源
最近更新 更多