Hafiza Hira Bashir
Hafiza Hira Bashir

Reputation: 23

My designed function is not looping over my input of sequences(x). It is returning the output of first sequence only

I am trying this and I'm not getting the required output which should be different for different sequences.

    oops <- function(x){
        aacom <- Peptides::aaComp(x)
        aacom
        c <- data.frame(aacom)
        y <- c %>% select(Number)
        Tiny_Number<- y[1,]
    return(Tiny_Number)
}

oops(c("GLFDIIKKIAESF","KWKLFKKIGAVLKVL")

Upvotes: 1

Views: 44

Answers (1)

Rui Barradas
Rui Barradas

Reputation: 76402

Try with map, since aaComp returns a list.

library(Peptides)
library(dplyr)
library(purrr)

oops <- function(x){
  aacom <- Peptides::aaComp(x)
  aacom %>%
    map(~as.data.frame(.)) %>%
    map(~select(., Number)) %>%
    map(~`[`(., 1, ))
}

oops(c("GLFDIIKKIAESF","KWKLFKKIGAVLKVL"))
#[[1]]
#[1] 3
#
#[[2]]
#[1] 2

Another version of the pipe.

oops <- function(x){
  aacom <- Peptides::aaComp(x)
  aacom %>%
    map(~as.data.frame(.) %>% select(., Number) %>% `[`(., 1, ))
}

Base R is simpler

Here is a function with no need for pipes and 3 calls to map. Its output is the same as above.

oops <- function(x){
  y <- Peptides::aaComp(x)
  lapply(y, '[', 1, 'Number')
}

Upvotes: 1

Related Questions