我有这个数据集:
structure(list(Event = structure(c(2L, 2L, 1L, 2L, 2L, 2L, 2L,
1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L), .Label = c("Insert",
"Ok"), class = "factor")), .Names = "Event", class = "data.frame", row.names = c(NA,
-18L))
每当有"插入"
时,我想在下面插入一个空行我如何在R?
中执行此操作答案 0 :(得分:5)
这是基于整数索引的第二种方法。在这里,我将使用一个字符向量,因为这对于提供的数据更有意义。
# get integer index with repeats for observations with "Insert"
myRows <- sort(c(seq_along(temp), which(temp == "Insert")))
# set second row index to missing
is.na(myRows) <- duplicated(myRows)
现在,将其提供给索引字符向量。
temp[myRows]
[1] "Ok" "Ok" "Insert" NA "Ok" "Ok" "Ok" "Ok" "Insert" NA "Insert" NA "Ok"
[14] "Ok" "Ok" "Ok" "Insert" NA "Ok" "Ok" "Ok" "Insert" NA
数据强>
temp <-
structure(list(Event = structure(c(2L, 2L, 1L, 2L, 2L, 2L, 2L,
1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L), .Label = c("Insert",
"Ok"), class = "factor")), .Names = "Event", class = "data.frame", row.names = c(NA,
-18L))
temp <- as.character(temp$Event)
答案 1 :(得分:1)
我们可以创建一个逻辑向量
i1 <- df1$Event == "Insert"
Event <- unlist(lapply(split(df1$Event,
cumsum(c(TRUE, i1[-length(i1)]))), function(x) c(as.character(x), "")))
df2 <- data.frame(Event, stringsAsFactors=FALSE)
或另一种选择是
library(data.table)
setDT(df1)[, grp := cumsum(shift(Event == "Insert", fill = TRUE))
][, .SD[c(seq_len(.N), .N+1)] , grp
][is.na(Event), Event := ""
][, grp := NULL][]
# Event
# 1: Ok
# 2: Ok
# 3: Insert
# 4:
# 5: Ok
# 6: Ok
# 7: Ok
# 8: Ok
# 9: Insert
#10:
#11: Insert
#12:
#13: Ok
#14: Ok
#15: Ok
#16: Ok
#17: Insert
#18:
#19: Ok
#20: Ok
#21: Ok
#22: Insert
#23:
答案 2 :(得分:0)
这是一个想法:
mydf <- structure(list(Event = structure(c(2L, 2L, 1L, 2L, 2L, 2L, 2L,
1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L), .Label = c("Insert",
"Ok"), class = "factor")), .Names = "Event", class = "data.frame", row.names = c(NA,
-18L))
mydf$rowindex <- 1:nrow(mydf)
mydf$repeats <- 1
mydf$repeats[which(mydf$Event=="Insert")] <- 2
mydf2 <- mydf[rep(mydf$rowindex,mydf$repeats),]
mydf2[which(grepl("\\.",row.names(mydf2))),"Event"] <- NA
如果有帮助,请告诉我。