# [R] condition and function

Berend Hasselman bhh at xs4all.nl
Mon May 13 08:55:07 CEST 2013

On 13-05-2013, at 08:16, catalin roibu <catalinroibu at gmail.com> wrote:

> Hello all!
> I want to create a new variable in a dataframe, and this variable is based
> on a regression equation. For example for example for the hc<=10, the
> values of the new variable(d0.3) is obtained by d0.3=15.9122+0.2105*dc, for
> hc<=30, d0,3 values are obtained by d0.3=1.4781+0.8827*dc.
>

There is no column with name dc in the dataframe.
So shouldn't that be dcm? And maybe hc<=30 should be hc>30?
Something like this perhaps

DF[["do.3"]] <- with(DF,ifelse(hc<=10,15.9122+0.2105*dcm, ifelse(hc<=30,1.4781+0.8827*dcm,NA)))

where NA values in hc will be propagated.

Please provide a less bulky example for the dataframe in future.

Berend

> Thank you!
>
> My values are like this:
> structure(list(dcm = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 28.5, NA, NA, NA,
> NA, 24.5, NA, NA, NA, NA, 27, NA, NA, NA, NA, NA, NA, NA, NA,
> NA, NA, 11, NA, 28, NA, NA, NA, NA, NA, NA, 25.5, 23, NA, NA,
> 23, NA, NA, NA, NA, NA, 22, NA, NA, NA, NA, NA, NA, NA, NA, NA,
> NA, NA, NA, NA, NA, NA, NA, 24.5, 24, NA, NA, NA, NA, NA, NA,
> NA, NA, NA, NA, NA, 18.5, NA, NA, 15, NA, NA, NA, 28, NA, NA,
> NA, NA, NA, NA, NA, 24.5, NA, NA, NA, NA, NA, NA, NA, NA, NA,
> NA, NA, NA, 19, NA, NA, NA, NA, NA, NA, NA, 18, 19.5, 17, NA,
> 20.5, 21.5, 17.5, 72, NA, 34, NA, NA, NA, 37, NA, NA, NA, 37,
> NA, NA, 44, 52, NA, NA, NA, NA, NA, NA, 36.5, NA, NA, 21, 22.5,
> NA, NA, NA, NA, NA, NA, NA, 31.5, NA, NA, NA, NA, NA, NA, NA,
> NA, NA, NA, NA, NA, NA, NA, 34, NA, NA, NA, NA, NA, NA, NA, 23,
> 26.5, NA, NA, 42, 26, NA, NA, 21.5, NA, 30, NA, 28, NA, NA, 36,
> 29, 20.5, NA, NA, 51.5, 28.5, 28.5, NA, NA, NA, NA, NA, NA, NA,
> 32, NA, 19, NA, 55, NA, 39.5, NA, 42.5, NA, 47, NA, 50.5, 65.5,
> NA, NA, NA, NA, 30.5, 32.5, NA, NA, 26.5, 35, NA, NA, NA, NA,
> NA, NA, 33, 32.5, 21, NA, 23.5, NA, NA, NA, NA, NA, NA, NA, 48,
> NA, 49, 29.5, 14.5, NA, NA, NA, 25.5, NA, NA, 45, NA, NA, NA,
> NA, NA, 64, 65, 26.5, NA, 63, 46.5, NA, 22.5, NA, 42, 59.5, NA,
> NA, NA, NA, NA, NA, NA, 23.5, NA, 33.5, NA, 44.5, 34.5, NA, NA,
> NA, NA, NA, NA, NA, NA, 25.5, NA, 14, NA, 19, NA, NA, NA, NA,
> NA, NA, 22.5, NA, NA, 17, NA, NA, NA, NA, NA, NA, NA, NA, 35,
> NA, NA, NA, NA, NA, NA, NA, 33, NA, 33, 35.5, NA, 32.5, 29, NA,
> 19.5, NA, 12, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
> NA, NA, NA, NA, NA, 54, NA, 32, 49, NA, NA, NA, 60, 26, NA, 56.5,
> NA, 54, 29, 35.5, NA, NA, NA, NA, NA, 37.5, NA, NA, NA, NA, NA,
> NA, NA, NA, NA, NA, 46, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
> 58, NA, NA, 27, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 25,
> 33, 32.5, NA, NA, NA, NA, 19, NA, 19, NA, NA, NA, NA, NA, NA,
> 26.5, NA, NA, NA, NA, NA, 25, NA, NA, NA, NA, NA, NA, NA, NA,
> 20.5, NA, NA, NA, NA, NA, NA, NA, 17, 25, 47, NA, NA, NA, 35.5,
> NA, 22.5, NA, NA, NA, NA, NA, NA, NA, NA, NA, 62, NA, NA, NA,
> NA, NA, NA, NA, 26, NA, NA, NA, NA, 22, 22.5, 21, 19.5, 28, NA,
> 23.5, 49, 48, 47, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
> NA, NA, 16, NA, NA, NA, NA, 54, 130, NA, NA, NA, NA, NA, NA,
> NA, NA, NA, 18, NA, NA, 25.5, NA, NA, NA, NA, NA, NA, NA, NA,
> NA, NA, NA, NA, NA, NA, NA, NA), hc = c(NA, NA, NA, NA, NA, NA,
> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
> NA, NA, NA, NA, NA, NA, NA, 8, NA, NA, NA, NA, NA, NA, NA, NA,
> NA, NA, 10, NA, NA, NA, NA, NA, 5, 5, NA, NA, 5, NA, NA, NA,
> NA, NA, NA, NA, NA, 5, NA, 5, 3, NA, NA, NA, 3, 5, NA, NA, NA,
> NA, NA, NA, 5, 5, NA, NA, NA, NA, NA, NA, NA, 3, NA, NA, 5, 5,
> 10, NA, NA, NA, NA, NA, NA, 8, 10, 10, NA, 15, 15, 15, 5, NA,
> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0, NA, NA, NA,
> 0, 0, NA, 3, 0, 0, NA, NA, NA, NA, NA, NA, NA, 0, NA, NA, NA,
> NA, NA, NA, NA, 0, NA, NA, NA, NA, NA, NA, NA, NA, 0, NA, NA,
> NA, NA, 0, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
> NA, NA, NA, NA, NA, 0, NA, NA, NA, 0, NA, NA, NA, NA, NA, NA,
> NA, NA, NA, NA, NA, NA, NA, 0, NA, 0, NA, 1.7, NA, 1, NA, 5,
> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0, NA, NA, NA,
> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
> NA, NA, NA, NA, 0, 0, NA, NA, 0, NA, NA, NA, NA, 0, 0, 0, NA,
> NA, 0, 0, NA, NA, NA, 0, 0, NA, NA, 0, NA, NA, NA, NA, NA, NA,
> NA, NA, NA, NA, NA, 0, NA, NA, NA, NA, 0, 0, NA, NA, NA, NA,
> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
> NA, NA, NA, 0, NA, NA, NA, NA, NA, NA, NA, 0, 0, NA, 0, 0, NA,
> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0, NA, NA, NA, NA,
> NA, 0, NA, NA, NA, NA, 0, NA, NA, NA, NA, NA, NA, 0, 0, NA, 0,
> NA, 0, 0, 0, NA, NA, 0, NA, NA, NA, 0, NA, NA, NA, NA, NA, NA,
> NA, NA, NA, 0, NA, NA, NA, NA, NA, NA, NA, NA, 0, NA, 0, NA,
> NA, 0, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
> NA, NA, NA, NA, NA, NA, 0, NA, NA, NA, NA, NA, NA, NA, NA, NA,
> NA, NA, NA, NA, NA, 0, 0, 0, NA, NA, NA, NA, NA, NA, NA, NA,
> NA, NA, NA, NA, 0, 0, 0, 0, 0, 0, NA, NA, NA, NA, NA, NA, NA,
> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0, NA, NA, NA, 0, 0,
> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
> NA, NA, NA, NA, NA, 0, NA, NA, NA, NA, NA, NA, NA)), .Names = c("dcm",
> "hc"), class = "data.frame", row.names = c(NA, -556L))
>
>
>
> --
> ---
> Catalin-Constantin ROIBU
> Lecturer PhD, Forestry engineer
> Forestry Faculty of Suceava
> Str. Universitatii no. 13, Suceava, 720229, Romania
> office phone     +4 0230 52 29 78, ext. 531
> mobile phone   +4 0745 53 18 01
>                       +4 0766 71 76 58
> FAX:                +4 0230 52 16 64
> silvic.usv.ro
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help