使用 R 操作数据 table
Manipulate data table with R
请编辑删除此内容,因为它已过时
使用which
获取Y = 0
的索引位置并对数据进行子集化。
inds <- which(df$Y == 0)
df <- df[inds[1]:inds[2], ]
df
# X Y
#2 2 0.0
#3 3 -0.2
#4 4 -0.4
#5 5 0.0
数据
df <- structure(list(X = 1:6, Y = c(-0.1, 0, -0.2, -0.4, 0, -0.2)),
class = "data.frame", row.names = c(NA, -6L))
我们可以使用 filter
和 cumsum
library(dplyr)
df %>%
filter(lag(cumsum(Y == 0)) < 2)
# X Y
#1 2 0.0
#2 3 -0.2
#3 4 -0.4
#4 5 0.0
数据
df <- structure(list(X = 1:6, Y = c(-0.1, 0, -0.2, -0.4, 0, -0.2)),
class = "data.frame", row.names = c(NA, -6L))
请编辑删除此内容,因为它已过时
使用which
获取Y = 0
的索引位置并对数据进行子集化。
inds <- which(df$Y == 0)
df <- df[inds[1]:inds[2], ]
df
# X Y
#2 2 0.0
#3 3 -0.2
#4 4 -0.4
#5 5 0.0
数据
df <- structure(list(X = 1:6, Y = c(-0.1, 0, -0.2, -0.4, 0, -0.2)),
class = "data.frame", row.names = c(NA, -6L))
我们可以使用 filter
和 cumsum
library(dplyr)
df %>%
filter(lag(cumsum(Y == 0)) < 2)
# X Y
#1 2 0.0
#2 3 -0.2
#3 4 -0.4
#4 5 0.0
数据
df <- structure(list(X = 1:6, Y = c(-0.1, 0, -0.2, -0.4, 0, -0.2)),
class = "data.frame", row.names = c(NA, -6L))