如何将连续变量转换为 r 中的分类变量
How to convert continuous variable to a categorical in r
我有一个名为 clean_test_master2 的 df,您可以访问 Here
stim_ending_t visbility soundvolume Opening_text m sd coefVar
<dbl> <dbl> <dbl> <chr> <dbl> <dbl> <dbl>
1 1 0 0 Now focus on the Image 1.70 1.14 0.670
2 1 0 0 Now focus on the Sound 1.57 0.794 0.504
3 1 0 1 Now focus on the Image 1.55 1.09 0.701
4 1 0 1 Now focus on the Sound 1.77 0.953 0.540
5 1 1 0 Now focus on the Image 1.38 0.859 0.621
6 1 1 0 Now focus on the Sound 1.59 0.706 0.444
7 1.5 0 0 Now focus on the Image 1.86 0.718 0.387
8 1.5 0 0 Now focus on the Sound 2.04 0.713 0.350
9 1.5 0 1 Now focus on the Image 1.93 1.00 0.520
10 1.5 0 1 Now focus on the Sound 2.14 0.901 0.422
我 运行 这个函数 is.factor
看看我的 df 的列是连续的还是离散的
我找到了答案here
f <- sapply(clean_test_master2, is.factor)
> f
stim_ending_t visbility soundvolume Opening_text m sd coefVar
FALSE FALSE FALSE FALSE FALSE FALSE FALSE
我不确定 false 在这里是什么意思?以及如何检查我的列是连续的、离散的还是分类的
问: 这里的重要问题是如何将 stim_ending_t
转换为分类,这样我就可以 运行 不同的分析,如方差分析(请参阅此问题 )。
我找到了这个教程 here,它解释了如何使用名为 cat
的函数,这个函数创建了一个单独的 df,但我确实想保持我的 df 不变。我需要在 df 中的列中进行更改。
在Tidyverse中,可以使用dplyr::mutate_at()
将多列的class改为因子:
clean_test_master2 <- clean_test_master2 %>%
mutate_at(c("stim_ending_t", "visbility", "soundvolume", "Opening_text"), as.factor)
sapply(clean_test_master2, is.factor)
> sapply(clean_test_master2, is.factor)
stim_ending_t visbility soundvolume Opening_text m sd coefVar
TRUE TRUE TRUE TRUE FALSE FALSE FALSE
您可能需要单独定义每一列(如果您的值范围适合单个类别,则使用 factor(x, levels = y, labels = z)
。
您只需修改 sapply
行,为要转换为因子的每一列使用 as.factor
而不是 is.factor
并将其写回该变量名称。
例如:
clean_test_master2$stim_ending_t <- sapply(clean_test_master2$stim_ending_t, as.factor)
我有一个名为 clean_test_master2 的 df,您可以访问 Here
stim_ending_t visbility soundvolume Opening_text m sd coefVar
<dbl> <dbl> <dbl> <chr> <dbl> <dbl> <dbl>
1 1 0 0 Now focus on the Image 1.70 1.14 0.670
2 1 0 0 Now focus on the Sound 1.57 0.794 0.504
3 1 0 1 Now focus on the Image 1.55 1.09 0.701
4 1 0 1 Now focus on the Sound 1.77 0.953 0.540
5 1 1 0 Now focus on the Image 1.38 0.859 0.621
6 1 1 0 Now focus on the Sound 1.59 0.706 0.444
7 1.5 0 0 Now focus on the Image 1.86 0.718 0.387
8 1.5 0 0 Now focus on the Sound 2.04 0.713 0.350
9 1.5 0 1 Now focus on the Image 1.93 1.00 0.520
10 1.5 0 1 Now focus on the Sound 2.14 0.901 0.422
我 运行 这个函数 is.factor
看看我的 df 的列是连续的还是离散的
我找到了答案here
f <- sapply(clean_test_master2, is.factor)
> f
stim_ending_t visbility soundvolume Opening_text m sd coefVar
FALSE FALSE FALSE FALSE FALSE FALSE FALSE
我不确定 false 在这里是什么意思?以及如何检查我的列是连续的、离散的还是分类的
问: 这里的重要问题是如何将 stim_ending_t
转换为分类,这样我就可以 运行 不同的分析,如方差分析(请参阅此问题
我找到了这个教程 here,它解释了如何使用名为 cat
的函数,这个函数创建了一个单独的 df,但我确实想保持我的 df 不变。我需要在 df 中的列中进行更改。
在Tidyverse中,可以使用dplyr::mutate_at()
将多列的class改为因子:
clean_test_master2 <- clean_test_master2 %>%
mutate_at(c("stim_ending_t", "visbility", "soundvolume", "Opening_text"), as.factor)
sapply(clean_test_master2, is.factor)
> sapply(clean_test_master2, is.factor)
stim_ending_t visbility soundvolume Opening_text m sd coefVar
TRUE TRUE TRUE TRUE FALSE FALSE FALSE
您可能需要单独定义每一列(如果您的值范围适合单个类别,则使用 factor(x, levels = y, labels = z)
。
您只需修改 sapply
行,为要转换为因子的每一列使用 as.factor
而不是 is.factor
并将其写回该变量名称。
例如:
clean_test_master2$stim_ending_t <- sapply(clean_test_master2$stim_ending_t, as.factor)