Paul Smith
Paul Smith

Reputation: 1

R String substitution to replace characters in range of columns (vectors)

So I have a table with 5 rows and 3 columns

>temp
  data1  data2  data3
1 35.5Â 410.8Â 327.2Â
2 32.7Â 406.9Â 281.3Â
3 30.3Â 410.3Â 288.2Â
4 27.6Â 403.1Â 273.6Â
5 27.3Â 364.2Â 236.8Â

I want to get rid of those pesky "Â"s and convert these factors to characters. So, I tried something like:

temp[,1:3] <- apply(temp[,1:3], 2, as.numeric)

but I got warning messages and all values coerced as NAs.

So then I tried:

temp[,1:3] <- sub("Â","",temp[,1:3])

but this did not work.

I can do this:

temp[,1] <- sub("Â","",temp[,1])

which does work, but I need to do this over a large range of columns in a larger data set. Is there a way to sub on range of columns, instead of individual vectors?

Upvotes: 0

Views: 73

Answers (1)

Pankaj Sharma
Pankaj Sharma

Reputation: 388

strsplit(temp$data1,"Â")

strsplit(temp$data2,"Â")

strsplit(temp$data3,"Â")

Hope this helps.....

Upvotes: 1

Related Questions