Message-ID: <50F52A13.9070301@ymail.com>
Date: 2013-01-15T10:06:11Z
From: Pascal Oettli
Subject: Need some help on Text manipulation.
In-Reply-To: <CA+dpOJk_scaAwVD8-M49vKrcrfu1kmkrTV3up=Ws0yc_r2CHGA@mail.gmail.com>
Hi,
If you only need "aa" to be replaced by "a", "bb" by "b" and so on, try
the following:
Dat$att <- as.factor(substr(Dat$att,1,1))
HTH,
Pascal
Le 15/01/2013 18:48, Christofer Bogaso a ?crit :
> 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.
>