如何删除序列中有两个或多个单词(彼此之后)的行?

时间:2019-05-13 14:57:22

标签: r dataframe design-patterns sequential pattern-mining

我想删除两个或两个以上相同单词的行,例如一个序列。这是要进行顺序模式挖掘分析。

  

我已经尝试过distinct()duplicated()函数,但这只会删除   整行。

r_seq_5 <- r_seq_5[!duplicated(r_seq_5),] # remove duplicates


   #       Su Score result ROI       next_roi  third_roi  four_roi   five_roi   
   #  1     1    90 high   Elsewhere Elsewhere Teacher    Teacher    Teacher   
   #  2     1    90 high   Elsewhere Teacher   Teacher    Teacher    Teacher   
   #  3     1    90 high   Teacher   Pen       Teacher    Elsewhere  Smartboard

这是表。如果老师在句子中是两次或三次,只要没有彼此相接,都没关系。

所需的结果是:

# 1     1    90 high   Teacher   Pen       Teacher    Elsewhere  Smartboard

2 个答案:

答案 0 :(得分:0)

您可以使用gather()来重新组合变量,然后构建一个循环,以与前一个相同的方式标识该值。

最后,使用spread()来重建您的初始结构。

df <- data.frame(
  row = 1:4,
  Su = 1,
  Score = 90,
  result = 'high',
  ROI  = c('A', 'A', 'B', 'A'),
  ROI2 = c('A', 'B', 'C', 'B'),
  ROI3 = c('B', 'B', 'A', 'C')
) %>% 
  gather(-(row:result), key = roi, value = value) %>% 
  arrange(row) %>% 
  mutate(repeated = 0)

for(i in 2:nrow(df)){
  if(df$row[i] == df$row[i-1] & df$value[i] == df$value[i-1])
    df$repeated[i] = 1
}

df %>% 
  group_by(row) %>% 
  mutate(repeated = sum(repeated)) %>% 
  filter(repeated == 0) %>% 
  select(-repeated) %>% 
  spread(key = roi, value = value)

#     row    Su Score result ROI   ROI2  ROI3 
#   <int> <dbl> <dbl> <fct>  <chr> <chr> <chr>
# 1     3     1    90 high   B     C     A    
# 2     4     1    90 high   A     B     C 

答案 1 :(得分:0)

为此,我发现将因子转换为数字很方便。这是我的第一步,因为要比较列的范围,此路径似乎不太费劲。

为此,我使用了for这个qdap包,因为在Macth中,我用NA替换了这些值。

library(dplyr)
library(qdap)
df <- data.frame(Su = rep(1,3),
                 Score = rep(90,3),
                 ROI = c("A", "A", "B"),
                 NETX_ROI = c("A", "B", "C"),
                 third_roi = rep("B", 3),
                 four_roi = c("B", "B", "A"),
                 five_roi = c("B", "B", "D"))
df
> df
  Su Score ROI NETX_ROI third_roi four_roi five_roi
1  1    90   A        A         B        B        B
2  1    90   A        B         B        B        B
3  1    90   B        C         B        A        D
df2 <- df
roi <- c("A", "B", "C", "D")
# A = Elsewhere
# B = Teacher
# C = Pen
# D = Smartboard

n <- seq(1, length.out = length(roi))
for (i in 1:length(n)) {
  df2[df2 == roi[i]] <- NA
  df2 <- qdap::NAer(df2, i)
}
> df2
  Su Score ROI NETX_ROI third_roi four_roi five_roi
1  1    90   1        1         2        2        2
2  1    90   1        2         2        2        2
3  1    90   2        3         2        1        4
df2 <- df2 %>% 
  dplyr::select(-c(Su, Score)) %>% 
  as.matrix()

nn <- ncol(df2)
x  <- matrix(nrow = nrow(df2), ncol = ncol(df2)-1)
for (i in 1:(nn-1)) {
  xx <- ifelse(df2[,i] == df2[,i+1], NA, 0)
  x[,i] <- as.matrix(xx)
}
> x
     [,1] [,2] [,3] [,4]
[1,]   NA    0   NA   NA
[2,]    0   NA   NA   NA
[3,]    0    0    0    0

最后,我刚刚删除了NA行。

dfx <- x %>% 
  as.data.frame()

df_test <- df %>% 
  dplyr::bind_cols(dfx) %>% 
  na.omit() %>% 
  dplyr::select(1:ncol(df))
df_test
> df_test
  Su Score ROI NETX_ROI third_roi four_roi five_roi
3  1    90   B        C         B        A        D