r - R:将多个特定变量转换为因子
问题描述
我想从更大的数据集中抽取 10 个变量,然后将数据集中的多个特定变量 (2:10) 从连续变量转换为因子,同时将变量 1 作为连续变量。这就是我所拥有的:
data <- read.csv("data.csv") %>%
as.tibble() %>%
complete() %>%
subset("1", "2", "3", "4", "5", "6", "7", "8", "9", "10") %>%
lapply([,2:10], factor)
我究竟做错了什么?
解决方案
dplyr >= 1.0.0
使用新的dplyr::across
:
library(dplyr)
data <- read.csv("data.csv") %>%
as.tibble() %>%
complete() %>%
dplyr::mutate(data, across(2:10, factor))
dplyr < 1.0.0
由于 _if, _at, _all 已被取代,这将适用于旧版本,但应使用上述内容。
library(dplyr)
data <- read.csv("data.csv") %>%
as.tibble() %>%
complete() %>%
dplyr::mutate_at(2:10, factor)
mutate_at
将应用于factor
列2:10
。
推荐阅读
- ionic4 - Stenciljs:路由到新内容并且不能更改背景
- java - 在java中使用RC4加密图像
- c# - 发送多个 httpclient 或 httpresponse 时 UI 挂起
- python - 如果进程(文件)由外部程序打开,如何使用 Python 关闭它?
- c# - UWP 如何使用 AudioGraph API 暂停录音?
- r - 如何选择 R 数据框中两列的所有唯一组合?
- node.js - 是否可以在语音通道中映射具有某些权限的人?不和谐.js
- google-data-studio - 对两个不同的数据源使用相同的过滤器控件
- javascript - 带有文本和值下拉列表的 Kendo UI 网格过滤器
- c# - 如何读取文件中的数据并将行保存到二维表中