[R] Need some help on Text manipulation.

arun smartpink111 at yahoo.com
Tue Jan 15 14:16:38 CET 2013


HI,
In this case, all the elements in Dat$att are found in Replace.
 Dat1<-within(Dat,{att<-as.character(att)})
vec1<-unlist(lapply(strsplit(Dat1$att,""),unique))
vec1
 #[1] "b" "b" "b" "d" "b" "b" "a" "c" "a" "d" "a" "b" "b" "b" "b" "d" "b" "b" "d"
#[20] "b"

 Dat1[5:7,2]<-c("uu","tt","vv")
vec1<-unlist(lapply(strsplit(Dat1$att,""),unique))

 Dat1$att<-ifelse(vec1%in%Replace,vec1,Dat1$att)
Dat1$att
# [1] "b"  "b"  "b"  "d"  "uu" "t"  "vv" "c"  "a"  "d"  "a"  "b"  "b"  "b"  "b" 
#[16] "d"  "b"  "b"  "d"  "b" 
A.K.






----- Original Message -----
From: Christofer Bogaso <bogaso.christofer at gmail.com>
To: r-help <r-help at r-project.org>
Cc: 
Sent: Tuesday, January 15, 2013 4:48 AM
Subject: [R] Need some help on Text manipulation.

Dear all,

Let say I have following data-frame:

Dat <- structure(list(dat = c(-0.387795842956327, -0.23270882099043,
-0.89528973290562, 0.95857175595512, 1.61680582493783, -1.17738110289352,
0.210601060411423, -0.827369747447338, -0.36896112964414, 0.440288648776096,
1.28018410608809, -0.897113649961341, 0.342216546981718, -1.17288066266219,
-1.57994101992621, -0.913655547602414, -2.54753726314408, -0.617703410989815,
-0.443272763891558, 0.359181170918489), att = structure(c(2L,
2L, 2L, 4L, 2L, 2L, 1L, 3L, 1L, 4L, 1L, 2L, 2L, 2L, 2L, 4L, 2L,
2L, 4L, 2L), .Label = c("aa", "bb", "cc", "dd"), class = "factor")),
.Names = c("dat",
"att"), row.names = c(NA, -20L), class = "data.frame"); Dat


Now I want to replace the 2nd column with:

Replace <- letters[1:20]

The rule is as follows:

Consider the 1st element of the 2nd colume: 'bb'. Now I need to see
which element of 'Replace' is contained in 'bb'? Obviously, this will
be 'b'. Therefore I need to replace "bb" with "b". And so on for all
elements of 2nd column.

This is obviously an straightforward example. However I have some more
complex problem of replacing like that. I want to get the idea how to
achieve that programmatically.

Can somebody help me?

Thanks and regards,

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list