r - 在 R 中的数据帧上使用 psych reverse.code()
问题描述
我正在尝试在具有数字和非数字列的数据框中的一列上使用 psych 包中的 reverse.code()。但是,当我尝试执行此操作时,出现错误:
Error in items %*% keys.d :
requires numeric/complex matrix/vector arguments
您对如何完成这项工作有任何建议吗?我正在为我的 R 功能差异很大的学生制作教程,所以代码越简单越好。这是一个给出相同错误的模拟数据集:
sample <- tibble(name = c("Joe", "Frank"),
item_1 = c(1, 1),
item_2 = c(1, 1),
item_3 = c(5, 5))
key <- c("item_3")
reverse.code(keys = key, items = sample, mini = 1, maxi = 5)
解决方案
如果我们select
的数据不包括第一列即character
列,它应该可以工作
library(psych)
reverse.code(keys = key, items =sample[-1], mini = 1, maxi = 5)
# item_1 item_2 item_3-
#[1,] 1 1 1
#[2,] 1 1 1
或者在一个%>%
library(dplyr)
sample %>%
select_if(is.numeric) %>%
reverse.code(keys = key, items = ., mini = 1, maxi = 5)
推荐阅读
- powerbi - 在 DAX 中计数/计算/求和行
- python - 将字符打印与特定值一样多次
- python - 使用 Python,选择最近 60 天内修改过的目录中的文件
- abap - 如何在双击事件中获取 SALV ALV 字段值?
- python - 在pytorch模型训练中,如何冻结、解冻和再次冻结一些参数?
- javascript - Power BI 自定义主题文件未与 Angular 一起应用
- javascript - 如何在响应对象NodeJs的模板文字中插入html标签
- php - 将选定行的 id 传递给刀片文件中的组件标签
- angular - 在孩子中使用父属性(高度,宽度)
- python - 如何使用 grequests 发布数据和下载图像