[R] Replace values in a dataframe

Bert Gunter bgunter.4567 at gmail.com
Wed Jun 17 20:12:47 CEST 2015


Is the following what you want:

(z is your data frame)

> change <-c("2","2.5")
> names(change) <- c("<4","<5")

(note: this can be automated using regular expressions and will work for
lots more values to change. Sarah's ifelse() solution is fine for the
example, but becomes too cumbersome (as she intimated) for more values.)

> z$newcol <- with(z,{
+   for(nm in names(change))Color[Color == nm]<-change[nm]
+   as.numeric(Color)
+ })
> z
    Color  Unit newcol
1:2     5 Hazen    5.0
1:3    <4 Hazen    2.0
1:4     5 Hazen    5.0
1:5    <5 Hazen    2.5
1:6     5 Hazen    5.0

Incidentally, if this is how you are attempting to deal with censored data,
it may be a very bad idea. For why, consult a local statistician or post on
a statistics list like stats.stackexchange.com

Cheers,
Bert


Bert Gunter

"Data is not information. Information is not knowledge. And knowledge is
certainly not wisdom."
   -- Clifford Stoll

On Wed, Jun 17, 2015 at 10:31 AM, Shane Carey <careyshan at gmail.com> wrote:

> Hey all,
>
> I have a dataframe that consists of:
>
> structure(list(Color = c("5", "<4","5", "<5", "5"), Unit = c("Hazen",
> "Hazen",
> "Hazen", "Hazen", "Hazen")), .Names = c("Color", "Unit"), row.names =
> c("1:2",
> "1:3", "1:4", "1:5","1:6"), class = "data.frame")
>
> I need to find the <4 and have a new column with the result of 4 ÷ 2 = 2
>
> Similarly
>
> I need to find the <5 and have the new column with the result of 5 ÷ 2 =
> 2.5
>
>
> All other numeric values would be added to the new column also to end up
> with:
>
>
>
>   Color New value Unit 1:2 5 5 Hazen 1:3 <4 2 Hazen 1:4 5 5 Hazen 1:5 <5
> 2.5 Hazen 1:6 5 5 Hazen
>
>
>
> Thanks for your help!!
>
> --
> Shane
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

	[[alternative HTML version deleted]]



More information about the R-help mailing list