在R中应用和附加数据框

时间:2017-05-09 16:49:44

标签: r matrix dataframe dplyr bind

我试图转换让我们说数据帧ALPHA:

     A    B    C     D      E
1 0.80 2.00 0.09 201.1 335.00

到数据帧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

所以几乎乘以2并附加。

目前这样做:

  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="")))

然而,它传递了curveCalculator ALL NAMES,因此对于A,它将传递0.80变量和c(&#34; A&#34;,&#34; B&#34;,&#34; C&#34;, &#34; D&#34;,&#34; E&#34;)用于变量名称。我希望它只能通过&#34; A&#34;对于A,&#34; B&#34;对于B等等..

1 个答案:

答案 0 :(得分:0)

试试这个

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