创建字符串列表

时间:2018-03-09 22:15:25

标签: r string list

我需要一些帮助,因为我无法弄清楚如何解决当前的问题。

我有一个字符串列表:

List[1]: "| EVENT_READ=>EVENT_EXIT Score: =8.28771237954946 | EVENT_READ=>EVENT_FORK Score: =8.0397848661059 | EVENT_CLOSE=>EVENT_EXIT Score: =8.07825901392049 | EVENT_CLOSE=>EVENT_FORK Score: =7.93016037493137 | EVENT_OPEN=>EVENT_EXIT Score: =8.24331826019101 | EVENT_OPEN=>EVENT_FORK Score: =8.0023101606872 | EVENT_LSEEK=>EVENT_EXIT Score: =8.48035745749183 |" 

List[2]: "| EVENT_READ,EVENT_LSEEK,EVENT_FORK=>EVENT_EXIT Score: =8.96578428466209 | EVENT_READ,EVENT_LSEEK,EVENT_EXIT=>EVENT_FORK Score: =8.42973138442187 |"

.....

我想要做的是使用管道List[i]作为分隔符拆分"|"中的每个字符串,并删除每个(Score: =xxx)字符串,然后检索生成的字符串列表对于我原始字符串的每个项目,即结果列表类似于:

List[1][1]=EVENT_READ=>EVENT_EXIT

List[1][2]=EVENT_READ=>EVENT_FORK

... and so one 

List[2][1]= EVENT_READ,EVENT_LSEEK,EVENT_FORK=>EVENT_EXIT
List[2][2]=EVENT_READ,EVENT_LSEEK,EVENT_EXIT=>EVENT_FORK 

我已尝试使用resultList=lapply(List,function(x){strsplit(x,split='|')})但似乎没用。

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

一个选项可能是在阅读lapply文件转化数据后使用greplregmatchessample.csv

lst <- read.csv("sample.csv", stringsAsFactors = FALSE)
ll <- lapply(lapply(lst, function(x)strsplit(x,split='\\|')),find_event)

# Function finds events, and collapse events together
find_event <- function(x){
  #if Score: tag is found then get EVENTS
  if(any(grepl("Score:", x))){
     # Find and get only EVENT
    mapply("paste0",lapply(x,  function(x) regmatches(x,
       regexpr("EVENT_\\w+=\\>\\w+", x, perl = TRUE))), collapse = ",")
  }else{
    mapply("paste0", x, collapse = ",")
  }
}

df <- data.frame(Objects = ll$Objects, RulesAndScores = ll$RulesAndScores)

# There will be 7 rows in above data.frame
df[1,1]
#[1] a1c0fcaf-7472-38a9-8148-bb5d76b381b3
df[1,2]
    #EVENT_READ=>EVENT_EXIT,EVENT_READ=>EVENT_FORK,EVENT_CLOSE=>EVENT_EXIT,EVENT_CLOSE=>EVENT_FORK,
#EVENT_OPEN=>EVENT_EXIT,EVENT_OPEN=>EVENT_FORK,EVENT_LSEEK=>EVENT_EXIT,EVENT_LSEEK=>EVENT_FORK,
#EVENT_CLOSE=>EVENT_EXIT,EVENT_CLOSE=>EVENT_FORK,EVENT_OPEN=>EVENT_EXIT,EVENT_OPEN=>EVENT_FORK,
 #..........<truncated>

答案 1 :(得分:0)

@MKR对不起,我在这里回答了你的问题,因为它说的信息太长,无法发表评论。

violators_file <- read.csv("./sample.csv") 
violatorsList=as.list(as.character(violators$Objects))
NbViolators=length(violatorsList)
association_rules=as.character(violators$RulesAndScores)
ListOfRules=lapply(sapply(association_rules, function(x)strsplit(x,split='\\|')),function(x) grep("^.((?!Score:).)*$", x, value=TRUE, perl = TRUE))