Avi
Avi

Reputation: 2283

Problems and error in strsplit

I use the following commands:

 sequence <-  '<{EADFE20F543836047330DEFFB893127AF536560121698ADE2FCE6985E07A40D8 SELECT;DD2E595CF23E65E128560B655E0C6848 SELECT}>'
v1 <- trimws(gsub('[[:punct:]]+', '', sapply(strsplit(sequence, '(?<=\\})(?=\\{)|[[,;]', perl=TRUE), tail, 1)))

I would like to get the whole string, however I get only part of the string:

v1 [1] "F73431225ED64969DC4BEBD06092FD6F SELECT"

The desired output is the content of the string between the <{ }>

What do I have to do in order to change it in order to get all the string?

In addition, If I use instead of the string sequence column of dataframe, I get the following error:

Error in strsplit(RES1$sequence, "(?<=\\})(?=\\{)|[[\\,\\;]", perl = TRUE) : 
  non-character argument

Here is the head of the RES1$sequence:

> head (RES1$sequence)
[1] <{EADFE20F543836047330DEFFB893127AF536560121698ADE2FCE6985E07A40D8 SELECT;DD2E595CF23E65E128560B655E0C6848 SELECT}>        
[2] <{F73431225ED64969DC4BEBD06092FD6F SELECT}>                                                                                
[3] <{88FFF14FDD46ED862DAEB36F8D0F6215 SELECT}>                                                                                
[4] <{1C9AAE933F916BA94B5D2B5FA320E05D85C780CD1A9922E26BC1FB7C422F42B2 SELECT}>                                                
[5] <{3FCC23C2562BE9926049EAF2D88CD3D4 SELECT;314CD91DCA8849C64DCEACBA2E3B65B7 SELECT;09E9146A444AE1C47B8E4139D6D69A48 SELECT}>
[6] <{184E7C8929FC9CEA72EF21D99CDC40D9 SELECT}>                                                                                
20 Levels: <{\\N}> ... <{F73431225ED64969DC4BEBD06092FD6F SELECT}>
> class (RES1)
[1] "data.frame"

Upvotes: 0

Views: 356

Answers (1)

Colonel Beauvel
Colonel Beauvel

Reputation: 31161

"The desired output is the content of the string between the <{ }>", why not simply:

gsub('<\\{(.*)\\}>', '\\1', sequence)
#[1] "EADFE20F543836047330DEFFB893127AF536560121698ADE2FCE6985E07A40D8 SELECT;DD2E595CF23E65E128560B655E0C6848 SELECT"

Upvotes: 1

Related Questions