[R] error when copy and transform within a data frame
Jeff Newmiller
jdnewmil at dcn.davis.ca.us
Mon Jul 19 16:19:14 CEST 2010
See ?ifelse.
Your num_value function needs to be rewritten in vector form using ifelse.
"Al R" <anevare1 at yahoo.com> wrote:
>
># trying to do a copy and a transform within a data frame, but getting the
>"arguments imply differing number of rows" error, and I'm not sure why
>
>a=c(1,2,3)
>b=c(2,3,4)
>c=c("Yes","No","Yes")
>d=c("No","Yes","No")
>
>df=data.frame(a,b,c,d)
>
># the following works fine!
>df = transform(df, new=sapply(df[,c(1,2)], FUN = function(x) { x^2 } ))
>
># but the following doesn't work:
>
>num_value = function(x) {
> if (x == "Yes") { return(1) }
> else if (x == "No") { return(0) }
> else return(NA)
>}
>
>df = transform(df, new=sapply(df[,c(3,4)], FUN = num_value ))
>
># generates this error..
>Error in data.frame(list(a = c(1, 2, 3), b = c(2, 3, 4), c = c(2L, 1L, :
> arguments imply differing number of rows: 3, 2
>
># thanks for the help!
>
>--
>View this message in context: http://r.789695.n4.nabble.com/error-when-copy-and-transform-within-a-data-frame-tp2293686p2293686.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>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.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
More information about the R-help
mailing list