简体   繁体   中英

Sapply and apply behaviour in R

Consider the following df :

structure(list(GID7173723 = c("A", "T", "G", "A", "G"), GID4878677 = c("G", 
"C", "G", "A", "G"), GID88208 = c("A", "T", "G", "A", "G"), GID346403 = c("A", 
"T", "G", "A", "G"), GID268825 = c("G", "C", "G", "A", "G")), row.names = c(NA, 
5L), class = "data.frame")

Looks like this:

  GID7173723 GID4878677 GID88208 GID346403 GID268825
1          A          G        A         A         G
2          T          C        T         T         C
3          G          G        G         G         G
4          A          A        A         A         A
5          G          G        G         G         G

And the following function:

f = function(x){
  ifelse(x=='A',x<-1,x) 
}

Using apply everything runs well as I expect:

    apply(df, 1,f)

          1   2   3   4   5  
GID7173723 "1" "T" "G" "1" "G"
GID4878677 "1" "C" "G" "1" "G"
GID88208   "1" "T" "G" "1" "G"
GID346403  "1" "T" "G" "1" "G"
GID268825  "1" "C" "G" "1" "G"

But if I use sapply or lapply all the values are converted to 1:

> sapply(dfn,f)
     GID7173723 GID4878677 GID88208 GID346403 GID268825
[1,]          1          1        1         1         1
[2,]          1          1        1         1         1
[3,]          1          1        1         1         1
[4,]          1          1        1         1         1
[5,]          1          1        1         1         1

I read from the documentation that lapply and sapply applies FUN to every element. Why is everything being converted to 1? Is there anything to do with R coercing integers to strings? Please help.

One other aspect which I don't understand is why with this new function I have only a vector with 5 elements instead of a dataframe filled with 'G':

    f2 = function(x) x<-'G'

> sapply(dfn,f2)
GID7173723 GID4878677   GID88208  GID346403  GID268825 
       "G"        "G"        "G"        "G"        "G" 

> apply(dfn, 1,f2)
  1   2   3   4   5 
"G" "G" "G" "G" "G" 
  • apply takes df or matrix as input, and outputs=>vector, list, array
  • lapply takes df, list or vector as input, and outputs=> list
  • sapply takes df, list or vector as input, and outputs=> vector or matrix

1st question. If you use

apply(df, 2,f)

you will also get a table full of 1s. R is not converting strings to integers.

2nd question. Try this as 1 is used for rows, and 2 is for columns.

apply(df, 2,f2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM