Saturday, 15 August 2015

r - Remove part of string using gsub -



r - Remove part of string using gsub -

i used below code remove part "(mv)" end of every string in vector (specifically row number 1 columns , skip column 1 shown in code), however, removed every m, v , mv in vector if @ start of string.

df[1,(-1)]<-gsub("[(mv)]","",df[1,(-1)])

how remove (mv) part @ end of each string without affecting rest of rest?

here reproducible example:

structure(list(x1 = structure(c(na, 5447), class = "date"), x2 = c("avon(mv)", "28.34"), x3 = c("ba.(mv)", "750.07"), x4 = c("cmrg(mv)", "10.040000000000001" ), x5 = c("cob(mv)", "143.22999999999999")), .names = c("x1", "x2", "x3", "x4", "x5"), row.names = c(na, -2l), class = "data.frame")

are sure first row not supposed column headers? that's not way info set , cause issues if need utilize numbers calculations.

anyway, remove (mv) each string, seek fixed argument in gsub, , create pattern "(mv)"

df[1,-1] <- gsub("(mv)", "", df[1,-1], fixed=true) df # x1 x2 x3 x4 x5 # 1 <na> avon ba. cmrg cob # 2 1984-11-30 28.34 750.07 10.040000000000001 143.22999999999999

but think need take @ info more closely because doesn't seem set correctly.

r gsub

No comments:

Post a Comment