【问题标题】:R: use of "where" to select rows by matching an element from a listR:使用“where”通过匹配列表中的元素来选择行
【发布时间】:2012-03-28 14:45:29
【问题描述】:

我试图在循环中使用 R 中的“where”函数根据匹配元素从两个数据集中挑选出某一行,然后制作两者的散点图。他们下面的代码示例提供了两个数据框和循环的行名。每个数据框包含的县多于“mycounties”中列出的县。

我之前在循环中使用过“where”函数,但这次R返回了“where”不存在的错误消息?我知道“哪里”存在!我的问题是,如何让 R 识别这种“位置”结构,或者有没有更好的方法来挑选我想要绘制的行?

 > names(dts)
 [1] "county"  "Freq125" "Freq126" "Freq127" "Freq128" "Freq129" "Freq130" "Freq131" "Freq132" "Freq133" "Freq134" "Freq135" "Freq136" "Freq137"
[15] "Freq138" "Freq139" "Freq140" "Freq141" "Freq142" "Freq143" "Freq144" "Freq145" "Freq146" "Freq147" "Freq148" "Freq149" "Freq150" "Freq151"
[29] "Freq152" "Freq153" "Freq154" "Freq155" "Freq156" "Freq157" "Freq158" "Freq159" "Freq160" "Freq161" "Freq162" "Freq163" "Freq164" "Freq165"
[43] "Freq166" "Freq167" "Freq168" "Freq169" "Freq170" "Freq171"

> names(pm)
 [1] "county" "pm125m" "pm126m" "pm127m" "pm128m" "pm129m" "pm130m" "pm131m" "pm132m" "pm133m" "pm134m" "pm135m" "pm136m" "pm137m" "pm138m"
[16] "pm139m" "pm140m" "pm141m" "pm142m" "pm143m" "pm144m" "pm145m" "pm146m" "pm147m" "pm148m" "pm149m" "pm150m" "pm151m" "pm152m" "pm153m"
[31] "pm154m" "pm155m" "pm156m" "pm157m" "pm158m" "pm159m" "pm160m" "pm161m" "pm162m" "pm163m" "pm164m" "pm165m" "pm166m" "pm167m" "pm168m"
[46] "pm169m" "pm170m" "pm171m"
> 
> mycounties = c("beaufort", "bertie", "bladen", "camden", "carteret", "chowan", "craven", 
+   "cumberland", "currituck", "dare", "duplin", "gates", "greene", "harnett", "hyde",
+   "jones", "lenoir", "new hanover", "onslow", "pamlico", "pasquotank", "pender", "perquimans", "pitt", "robeson", "sampson", "tyrrell", "washington")
> 
> LOOP
> for (i in 1:length(mycounties)) {
+ x = pm[where(pm$county == mycounties[i]),2:48]
+ y = dt[where(dts$county == mycounties[i]),2:48 ]
+ plot( x, y, main=paste("HYSPLIT & ED Visits", counties[i], sep=""),
+  xlab="HYSPLIT", ylab="ED Visits", pch=19)
+ } 
Error in `[.data.frame`(pm, where(pm$county == mycounties[i]), 2:48) : 
  could not find function "where"

【问题讨论】:

    标签: r loops plot where-clause


    【解决方案1】:

    就像 Jeff 和 cmets 所说,没有 where 函数。 which 是 R 版本。

    但是,在此示例中,您根本不需要 which

    x <- pm[pm$county == mycounties[i], 2:48]
    y <- dt[dt$county == mycounties[i], 2:48]
    

    在你的例子中也是if 声明:

    if (mat$county[i] %in% dt5$county)) {
        mat[i,2:58] = dt5[dt5$county == mat$county[i], 2:58]
    }
    

    假设您的dt5$county 与每个mat$county[i] 仅匹配一个县。否则会报错。

    【讨论】:

      【解决方案2】:

      您是否尝试引用which() 函数?

      【讨论】:

      • 不,“在哪里”。这是我之前在类似循环中运行过的一行示例,效果很好: if (mat$county[i] %in% list(dt5$county)) {mat[i,2:58] = dt5[其中(mat$county[i] == dt5$county), 2:58]}
      • 当您运行该行时,要么有人在别处定义了“where”函数,要么条件从未执行(即,(mat$county[i] %in% list(dt5$county)) 从未为真。where 肯定不是内置的在 R.which 中的函数是。
      • 有趣!所以我之前使用的循环肯定产生了不正确的结果......我将不得不回去交叉检查它。谢谢! Sooooo 在这种情况下可以使用“which”吗?我会试试?哪个
      • 是的。看起来which 可以满足您的需求。我不熟悉where 函数。
      • 事实上,我几乎可以肯定你给出的条件永远不会是真的。我假设mat 是一个矩阵,dt5 是一个数据框。在这种情况下,您的意思是使用mat$county[i] %in% dt5$county,而不是(mat$county[i] %in% list(dt5$county)),这绝不是真的。
      猜你喜欢
      • 2013-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-12
      • 1970-01-01
      相关资源
      最近更新 更多