简体   繁体   中英

create list of list of strings

I need some help, since I can't figure out how to resolve my current problem.

I have a list of strings:

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 |"

.....

What I want to do is to split each string in List[i] using the pipe "|" as separator, and delete each string which is (Score: =xxx) , and then retrieve the resulting list of strings for each item of my original string, ie, the resulting list would be similar to:

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 

I've tried some thing with resultList=lapply(List,function(x){strsplit(x,split='|')}) but seems to be not useful.

Thank you for your help.

One option could be to use lapply , grepl and regmatches after reading the sample.csv file transforming data.

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>

@MKR I am sorry I put the answer to your question here since it says the message is too long for a comment.

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM