Domanda

I have a vector like below

vec <- c("abc\edw\www", "nmn\ggg", "rer\qqq\fdf"......)

I want to remove everything after as soon as first slash is encountered, like below

newvec <- c("abc","nmn","rer")

Thank you.

My original vector is as below (only the head)

[1] "peoria ave\nste \npeoria"                      [2]   "wood dr\nphoenix"                                  
"central ave\nphoenix"                            
[4] "southern ave\nphoenix"                         [5]   "happy valley rd\nste   
\nglendaleaz "               "the americana at brand\n americana way\nglendale"

Here the problem is my original csv file does not contain backslashes, but when i read it backslashes appear. Original csv file is as below

[1] "peoria ave               [2] "wood dr
     nste                          nphoenix"       
     npeoria"

As you can see, they are actually separated by "ENTER" but when i read it in R using read.csv() they are replaced by backslashes.

È stato utile?

Soluzione

another solution :

 sub("\\\\.*", "", x)

Altri suggerimenti

vec <- c("abc\\edw\\www", "nmn\\ggg", "rer\\qqq\\fdf")
sub("([^\\\\])\\\\.*","\\1", vec)
[1] "abc" "nmn" "rer"

strssplit(vec, "\\\\") should do the job.

TO select the first element [[1]][1] 2nd [[1]][2]

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top