Assume I have a dataframe
mydata <- c("10 stack"," 10 stack and x" , "10 stack / dd" ," 10 stackxx")
R>mydata
[1] " 10 stack"
[2] " 10 stack and x"
[3] " 10 stack / dd"
[4] " 10 stackxx"
what I want to do is to replace and word begin with 10 stack [anything]to any other words in the dataframe , but without removing the rest of the string the desired output. Also replace the backslash with and or comma.
[1] " new"
[2] " new and x"
[3] " new and dd"
[4] " new"
my code is
mydata[mydata =="10 stack" ] <- new # I can replace one type, but I need faster operation.
mydata[mydata =="///" ] <- and #for replacing backslash with and
I found another method can solve the problem
mydata<-as.data.frame(sapply(mydata,gsub,pattern="//\",replacement=","))
10 stackxxwithnew?10withnewor10 stack[anything]withnew