r - `$<-.data.frame`(`*tmp*`, conf.int, value = list(X1 = c(-0.305562185432494, : 替换有 2 行,数据有 20
问题描述
我想在数据框中添加 t.test 函数 conf.int 的输出,使用下面的代码
y <- data.frame(matrix(rnorm(1000),20))
test <- apply(y, 1, t.test, alternative = c("two.sided", "less", "greater"),mu = 0, paired = FALSE, var.equal = FALSE,conf.level = 0.95)
y$conf.int <-data.frame(sapply(test,function(x)c(x$conf.int)))
然而它返回
Error in `$<-.data.frame`(`*tmp*`, w, value = list(X1 = c(-0.305562185432494, : replacement has 2 rows, data has 20
我搜索了解决方案,然后修改了下面给出的代码
y$x1_range <- NA
y$x1_range[which(y$x1 <= 2)] <-data.frame(sapply(test,function(x)c(x$conf.int)))
但它只创建空列,没有别的。
提前致谢
解决方案
您可以从中提取'conf.int'
以test
获取 2 列矩阵和cbind
原始y
.
y <- data.frame(matrix(rnorm(1000),20))
test <- apply(y, 1, t.test, alternative = c("two.sided", "less", "greater"),
mu = 0, paired = FALSE, var.equal = FALSE,conf.level = 0.95)
result <- cbind(y, t(sapply(test, `[[`, 'conf.int')))
推荐阅读
- ios - 我想制作一个与漫威所有事物相关的每日随机事实应用程序。这将是大量的事实,那么存储所有数据的最佳方式是什么?
- mysql - 使用 MySQL Workbench 添加时字符串值不正确
- javascript - 获取 rangeError 超出最大调用堆栈大小
- teamcity - 标记提交/构建以进行部署
- sapb1 - SAP B1 - 组件项目不能是虚拟错误
- google-maps - 切换图层谷歌地图 API 不起作用
- css - 带有 Bootstrap 4 的数据表看起来很乱
- c# - 我需要从形成列表的控制台输入中删除空格
- javascript - 跨域 AJAX 得到响应,但在错误消息中
- go - 使用 Websocket 向客户端广播服务器端消息