[R] confused by lapply

Sam Steingold sds at gnu.org
Wed Feb 16 18:42:43 CET 2011


Description:

     'lapply' returns a list of the same length as 'X', each element of
     which is the result of applying 'FUN' to the corresponding element
     of 'X'.

I expect that when I do
> lapply(vec,f)
f would be called _once_ for each component of vec.

this is not what I see:

parse.num <- function (s) {
  cat("parse.num1\n"); str(s)
  s <- as.character(s)
  cat("parse.num2\n"); str(s)
  if (s == "N/A") return(s);
  as.numeric(gsub("M$","e6",gsub("B$","e9",s)));
}


> vec
     mcap
1  200.5B
2   19.1M
3  223.7B
4  888.0M
5  141.7B
6  273.5M
7 55.649B
> str(vec)
'data.frame':	7 obs. of  1 variable:
 $ mcap: Factor w/ 7 levels "141.7B","19.1M",..: 3 2 4 7 1 5 6
> vec<-lapply(vec,parse.num)
parse.num1
 Factor w/ 7 levels "141.7B","19.1M",..: 3 2 4 7 1 5 6
parse.num2
 chr [1:7] "200.5B" "19.1M" "223.7B" "888.0M" "141.7B" "273.5M" ...
Warning message:
In if (s == "N/A") return(s) :
  the condition has length > 1 and only the first element will be used

i.e., somehow parse.num is called on the whole vector vec, not its
components.

what am I doing wrong?

-- 
Sam Steingold (http://sds.podval.org/) on CentOS release 5.3 (Final)
http://dhimmi.com http://mideasttruth.com http://truepeace.org
http://camera.org http://memri.org http://palestinefacts.org http://iris.org.il
Despite the raising cost of living, it remains quite popular.



More information about the R-help mailing list