将 sapply 的输出存储到数据框中? [英] Store output of sapply into a data frame?
问题描述
如何将 sapply()
的输出存储到数据帧,其中索引值存储在第一列中,其值存储在相应的第二列中.为了说明起见,我在这里只显示了 2 个元素,但我的数据中有 110 列.loan"是数据框.
how can I store the output of sapply()
to a dataframe where the index value is stored in first column and its value in corresponding 2nd column. For illustration, I have shown only 2 elements here, but there are 110 columns in my data. "loan" is the data frame.
cols <- sapply(loan,function(x) sum(is.na(x)))
cols
id
0
member_id
7
我希望输出为:
var value
id 0
member_id 7
我知道 sapply() 返回一个向量,但是当我打印该向量时,值会与其一些索引"一起打印,例如列名(如果应用于数据框).那么,现在当我想将它存储为具有两列的数据框时,其中第一列包含索引部分,第二列包含值,我该怎么做?
I know that sapply() returns a vector, but when I print the vector, values are printed along with its some "index" e.g., column name if applied on a data frame. So, now when I want to store it as a data frame with two columns where 1st column contains the index part and the second column contains the value, how can I do it?
推荐答案
我找到了问题的答案.对于那些真正理解我的问题的人来说,这个答案可能是有道理的:
I found an answer to my question. For those who actually did understand my problem, this answer might make sense:
cols <- data.frame(sapply(loan ,function(x) sum(is.na(x))))
cols <- cbind(variable = row.names(cols), cols)
我希望 row.names
位于与从 sapply
获得的值相对应的同一数据框的列中.
I wanted the row.names
to be in a column of the same data frame corresponding to the values obtained from sapply
.
这篇关于将 sapply 的输出存储到数据框中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!