简体   繁体   中英

Apply and append data frame in R

I am trying to convert lets say dataframe ALPHA:

     A    B    C     D      E
1 0.80 2.00 0.09 201.1 335.00

to dataframe BETA

     A    B    C     D      E   A1   B1   C1    D1     E1  
1 0.80 2.00 0.09 201.1 335.00 1.60 3.00 0.18 402.2 670.00

so pretty much multiplies by 2 and appends.

Currently doing it as:

  curveCalculator <- function(variable, variableName){
       // Need variableName here for another part
       return(variable*2)
  }


  BETA <- lapply(ALPHA, function(variableName, variable){
    calculated <- curveCalculator(variable, variableName)
    return(calculated)
  }, names(optional))

  bind_cols(ALPHA, as.data.frame(BETA,col.names=paste(names(BETA), 1, sep="")))

However, it passes curveCalculator ALL NAMES, so for A it would pass 0.80 for variable and c("A","B","C","D","E") for variable name. I want it to only pass "A" for A, "B" for B and so on..

Try this

library(purrr)
BETA <- map2(ALPHA, names(ALPHA), curveCalculator) %>% 
    as.data.frame()
names(BETA) <- paste0(names(BETA), 1)
cbind(ALPHA, BETA)

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