这是我的数据框,我的原始数据框要大得多,但这仅是示例:
df<-structure(list(ind = structure(c(16437, 16437, 16437, 16437,
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437,
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437,
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437,
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437,
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437,
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437,
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437,
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437,
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437,
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437,
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437,
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437,
16437, 16437, 16437, 16437, 16437, 16437, 16437, 16437, 16440,
16440, 16440, 16440, 16440, 16440, 16440, 16440, 16440, 16440,
16440, 16440, 16440, 16440, 16440, 16440, 16440, 16440, 16440,
16440, 16440, 16440, 16440, 16440, 16440, 16440, 16440, 16440,
16440, 16440), class = "Date"), date = structure(c(17810, 17831,
17852, 17873, 17894, 17915, 17936, 17957, 17978, 17999, 18020,
18041, 18062, 18083, 18104, 18125, 18146, 18167, 18188, 18209,
18230, 18251, 18272, 18293, 18314, 18335, 18356, 18377, 18398,
18419, 18440, 18461, 18482, 18503, 18524, 18545, 18566, 18587,
18608, 18629, 18650, 18671, 18692, 18713, 18734, 18755, 18776,
18797, 18818, 18839, 18860, 18881, 18902, 18923, 18944, 18965,
18986, 19007, 19028, 19049, 19070, 19091, 19112, 19133, 19154,
19175, 19196, 19217, 19238, 19259, 19280, 19301, 19322, 19343,
19364, 19385, 19406, 19427, 19448, 19469, 19490, 19511, 19532,
19553, 19574, 19595, 19616, 19637, 19658, 19679, 19700, 19721,
19742, 19763, 19784, 19805, 19826, 19847, 19868, 19889, 19910,
19931, 19952, 19973, 19994, 20015, 20036, 20057, 20078, 20099,
20120, 20141, 20162, 20183, 20204, 20225, 20246, 20267, 20288,
20309, 17810, 17831, 17852, 17873, 17894, 17915, 17936, 17957,
17978, 17999, 18020, 18041, 18062, 18083, 18104, 18125, 18146,
18167, 18188, 18209, 18230, 18251, 18272, 18293, 18314, 18335,
18356, 18377, 18398, 18419), class = "Date"), m = c(21L, 42L,
63L, 84L, 105L, 126L, 147L, 168L, 189L, 210L, 231L, 252L, 273L,
294L, 315L, 336L, 357L, 378L, 399L, 420L, 441L, 462L, 483L, 504L,
525L, 546L, 567L, 588L, 609L, 630L, 651L, 672L, 693L, 714L, 735L,
756L, 777L, 798L, 819L, 840L, 861L, 882L, 903L, 924L, 945L, 966L,
987L, 1008L, 1029L, 1050L, 1071L, 1092L, 1113L, 1134L, 1155L,
1176L, 1197L, 1218L, 1239L, 1260L, 1281L, 1302L, 1323L, 1344L,
1365L, 1386L, 1407L, 1428L, 1449L, 1470L, 1491L, 1512L, 1533L,
1554L, 1575L, 1596L, 1617L, 1638L, 1659L, 1680L, 1701L, 1722L,
1743L, 1764L, 1785L, 1806L, 1827L, 1848L, 1869L, 1890L, 1911L,
1932L, 1953L, 1974L, 1995L, 2016L, 2037L, 2058L, 2079L, 2100L,
2121L, 2142L, 2163L, 2184L, 2205L, 2226L, 2247L, 2268L, 2289L,
2310L, 2331L, 2352L, 2373L, 2394L, 2415L, 2436L, 2457L, 2478L,
2499L, 2520L, 21L, 42L, 63L, 84L, 105L, 126L, 147L, 168L, 189L,
210L, 231L, 252L, 273L, 294L, 315L, 336L, 357L, 378L, 399L, 420L,
441L, 462L, 483L, 504L, 525L, 546L, 567L, 588L, 609L, 630L)), row.names = c(NA,
-150L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"
), vars = "ind", drop = TRUE, indices = list(0:119, 120:149), group_sizes = c(120L,
30L), biggest_group_size = 120L, labels = structure(list(ind = structure(c(16437,
16440), class = "Date")), row.names = c(NA, -2L), class = "data.frame", vars = "ind", drop = TRUE))
我只想从“ m”列中选择此值,然后按“ m”排列,这是我设置的命令:
df<-arrange(filter(df, m =="21"|m=="63"|m=="126"|m=="189"),m)
Error in is.data.frame(df) : object 'm' not found
还有其他方法可以得到相同的结果吗?以及为什么我有此错误消息?
我在做什么错??
谢谢
答案 0 :(得分:4)
这对我有用。您需要在运行以下代码之前重新启动R会话
library(dplyr)
df %>%
filter(m == "21" | m == "63" | m == "126" | m == "189") %>%
arrange(m)
# or
df %>%
filter(m %in% c(21, 63, 126, 189) %>%
arrange(m)
#> # A tibble: 8 x 3
#> # Groups: ind [2]
#> ind date m
#> <date> <date> <int>
#> 1 2015-01-02 2018-10-06 21
#> 2 2015-01-05 2018-10-06 21
#> 3 2015-01-02 2018-11-17 63
#> 4 2015-01-05 2018-11-17 63
#> 5 2015-01-02 2019-01-19 126
#> 6 2015-01-05 2019-01-19 126
#> 7 2015-01-02 2019-03-23 189
#> 8 2015-01-05 2019-03-23 189
如果我们仅加载plyr
包,我们将收到与OP相同的错误消息
library(magrittr)
library(plyr)
df %>%
filter(m == "21" | m == "63" | m == "126" | m == "189") %>%
arrange(m)
#> Error in filter(., m == "21" | m == "63" | m == "126" | m == "189"): object 'm' not found
由reprex package(v0.2.1.9000)于2018-09-21创建