Try this as a solution: > df <- data.frame(a=letters[15:17], b=c("21","NA","23"), c=10:12, d=15:17) > # convert to numeric > x <- as.matrix(df) > mode(x) <- "numeric" Warning message: In eval(expr, envir, enclos) : NAs introduced by coercion > cbind(df, RTot=rowSums(x, na.rm=TRUE)) a b c d RTot 1 o 21 10 15 46 2 p NA 11 16 27 3 q 23 12 17 52 >
On Tue, Sep 2, 2008 at 5:50 PM, Ralikwen <[EMAIL PROTECTED]> wrote: > > Hi, > I am new to R. > I have a dataframe with many columns some of which contain genuine strings > some numerical values as strings. The dataframe is created by cast so I have > no control over the resulting data type. > I want to attach columns as aggregates of other columns to the dataframe. > Here is the solution that I came up with (after a lot of struggle): > > castNum <- function(n) { > x<-as.numeric(as.character(n)) > if (is.na(x)){ > return(n) > }else{ > return(x) > } > } > df <- data.frame(a=letters[15:17], b=c("21","NA","23"), c=10:12, d=15:17) > cbind(df,RTot=rowSums(as.data.frame(lapply(df, function(x) > castNum(x)))[2:4],na.rm=TRUE)) > > This works, but is full of warnings and looks extremely ugly. > Could you direct me how to achieve the same result in a more elegant way? > > Thx. > Balázs > > > > -- > View this message in context: > http://www.nabble.com/converting-values-of-a-dataframe-to-numeric-%28where-possible%29-tp19279139p19279139.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. > -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? ______________________________________________ R-help@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.