当前要求是汇总单个列,并为每行提供一个计数。我遇到一些需要协助的问题:
所以我想做以下事情:
TextData
Message : @p_id is not valid
Message : @p_id is not valid
Message : @p_id is not valid
Message : @p_id is not valid
Message : ID record does not exist: @p_Id=11933
Message : ID record does not exist: @p_Id=21944
Message : ID record does not exist: @p_Id=31933
Message : ID record does not exist: @p_Id=41931
Message : ID record does not exist: @p_Id=51993
The duplicate key value is (129).
The duplicate key value is (129).
The duplicate key value is (135).
匹配并计入以下内容:
Count TextData Values
4 Message : @p_id is not valid
5 Message : ID record does not exist: @p_Id= 11933,21944,31933,41931,51993
3 The duplicate key value is (). 129,135
如果这不可能,那么至少进入此
Count TextData
4 Message : @p_id is not valid
5 Message : ID record does not exist: @p_Id=
3 The duplicate key value is ().
我已经搜索了数小时,试图为诸如此类的问题找到解决方案,但没有找到一个适合我或适合我的情况的示例。
答案 0 :(得分:2)
data.table解决方案
library( data.table )
library( stringr )
#read data
dt <- fread(
"Message : @p_id is not valid
Message : @p_id is not valid
Message : @p_id is not valid
Message : @p_id is not valid
Message : ID record does not exist: @p_Id=11933
Message : ID record does not exist: @p_Id=21944
Message : ID record does not exist: @p_Id=31933
Message : ID record does not exist: @p_Id=41931
Message : ID record does not exist: @p_Id=51993
The duplicate key value is (129).
The duplicate key value is (129).
The duplicate key value is (135).", header = FALSE, sep = "")
#see if a string with numbers is present in the text, if so: extract
dt[, `:=`( id = stringr::str_extract( V1, "\\d+" ),
V1 = ifelse( grepl ( "\\d+", V1 ), gsub( "\\d+", "", V1 ), V1 ) ) ]
#summarise
dt[, list( Count = .N, values = toString( unique( id ) ) ), by = V1][]
# V1 Count values
# 1: Message : @p_id is not valid 4 NA
# 2: Message : ID record does not exist: @p_Id= 5 11933, 21944, 31933, 41931, 51993
# 3: The duplicate key value is (). 3 129, 135