将函数的输出存储到数据框的新列中

store output of function into a new column of dataframe

我在下面有这段代码。我想将 y[1] + (y[2]/60) 的输出推入 x 中的新列。这可能真的很简单,我没有看到。谢谢您的帮助。

read.table(file=file.choose(),header=T)
x$AverageTime<- as.character(x$AverageTime)
sapply(strsplit(x$Average,":"),
       function(y) {
         y <- as.numeric(y)
     y[1] + (y[2]/60)
       }
)

您应该可以在 sapply 命令之前输入 "x$newcolumn <- " 以获取新列。

read.table(file=file.choose(),header=T)
x$AverageTime<- as.character(x$AverageTime)
x$newcolumn <- sapply(strsplit(x$Average,":"),
                     function(y) {y <- as.numeric(y)
                                  y[1] + (y[2]/60)})