[R] error when copy and transform within a data frame
Al R
anevare1 at yahoo.com
Mon Jul 19 10:46:27 CEST 2010
# 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.
More information about the R-help
mailing list