如何将列表中数据帧的逻辑重新编码为 R 中的数字?
我想将列表中数据帧的逻辑重新编码为 TRUE = 1 和 FALSE = 0 等数字。我该怎么做?
library(dplyr)
set.seed(94756)
mat1 <- matrix(sample(seq(-1,100, 0.11),50, replace = TRUE),ncol = 5)
mat1 <- as_tibble(mat1)
mat2 <- matrix(sample(seq(-1,100, 0.11),50, replace = TRUE),ncol = 5)
mat2 <- as_tibble(mat2)
mat3 <- matrix(sample(seq(-1,100, 0.11), 50,replace = TRUE),ncol = 5)
mat3 <- as_tibble(mat3)
data <- list(mat1, mat2, mat3)
data1 <- purrr::map(data, ~tibble::add_column(., V1_logical = between(.$V2, 20, 60), .after = 'V1'))
r_pre <- lapply(data1, "[", 2)
提前致谢!
I would like to recode logicals of dataframes within a list into numbers like TRUE = 1 and FALSE = 0. How can I do that?
library(dplyr)
set.seed(94756)
mat1 <- matrix(sample(seq(-1,100, 0.11),50, replace = TRUE),ncol = 5)
mat1 <- as_tibble(mat1)
mat2 <- matrix(sample(seq(-1,100, 0.11),50, replace = TRUE),ncol = 5)
mat2 <- as_tibble(mat2)
mat3 <- matrix(sample(seq(-1,100, 0.11), 50,replace = TRUE),ncol = 5)
mat3 <- as_tibble(mat3)
data <- list(mat1, mat2, mat3)
data1 <- purrr::map(data, ~tibble::add_column(., V1_logical = between(.$V2, 20, 60), .after = 'V1'))
r_pre <- lapply(data1, "[", 2)
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以直接将逻辑值更改为整数值。
tidyverse
版本将是 -You can change the logical values to integer values directly.
tidyverse
version would be -lapply(r_pre,\(x) mutate(x, V1_逻辑 = V1_逻辑^1))
输出:
lapply(r_pre,\(x) mutate(x, V1_logical = V1_logical^1))
Output:
另一种可能的解决方案:
Another possible solution:
使用 pull() 将 tibble 转换为向量:
将逻辑转换为数字:FALSE 将为 0,TRUE 将为 1:
从向量转换为 tibble:
Use pull() to convert tibble to vector:
Convert logical to numeric: FALSE will be 0, TRUE will be 1:
Convert from vector to tibble: