[R] How to create a new variable based on parts of another character variable.
Petr PIKAL
petr.pikal at precheza.cz
Mon Oct 24 15:00:57 CEST 2011
Hi
If you want to get rid of regular expressions at all and your A values
start AWI for Arctic and UFT for boreal you can
DF$D <- ifelse(substr(DF$A, 1,1) == "A", "Arctic", "Boreal")
Regards
Petr
>
> Hello,
> I am just starting with R and I am having a (most probably) stupid
problem
> by creating a new variable in a data.frame based on a part of another
> character variable.
>
> I have a data frame like this one:
>
>
> A B C
> AWI-test1 1 i
> AWI-test5 2 r
> AWI-tes75 56 z
> UFT-2 5 I
> UFT56 f t
> UFT356 9j t
> etc. etc. 89 t
>
>
> I now want to look in the variable A if the string AWI is present and
then
> create a variable D and putting "Arctic" inside. However, if the string
> UFT occurs in the variable A, then the variable D shall be "Boreal" etc.
etc.
>
> The resulting data.frame file should look like
> A B C D
> AWI-test1 1 i Arctic
> AWI-test5 2 r Arctic
> AWI-tes75 56 z Arctic
> UFT-2 5 I Boreal
> UFT56 f t Boreal
> UFT356 9j t Boreal
> etc. etc. 89 t
>
>
> I know how to do this when I want to look for the entire string of A
means
> when there is "AWI-test1" and then create the variable D with "Arctic"
but
> not how to look only for a substring in A?
> Would be great if somebody might help.
> Thanks
> Philipp
>
>
>
> ***************************************************
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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