简体   繁体   English

如何删除 R 中具有分类值的多个列?

[英]how to drop multiple column which has categorical values in R?

I know how to drop columns by name, but I am not quite sure how I am going to drop the columns which has categorical values.我知道如何按名称删除列,但我不太确定如何删除具有分类值的列。 It can be done manually looking at which columns has categorical values, but not intuitive for me using R code.可以手动查看哪些列具有分类值,但使用 R 代码对我来说并不直观。 How to detect columns which has categorical values?如何检测具有分类值的列? any way to make this happen?有什么办法可以做到这一点?

minimal data最小数据

mydf=structure(list(taken_time = c(15L, 5L, 39L, -21L, 46L, 121L), 
    ap6xl = c(203.2893857, 4.858269406, 2, 14220, 218.2215352, 
    115.5227706), pct5 = c(732.074484, 25.67901235, 1.01, 120.0477168, 
    3621.328567, 79.30561111), crp4 = c(196115424.7, 1073624.455, 
    1.23, 1457496.474, 10343851.7, 81288042.73), age = c(52L, 
    74L, 52L, 67L, 82L, 67L), gender = structure(c(2L, 2L, 2L, 
    1L, 2L, 1L), .Label = c("F", "M"), class = "factor"), inpatient_readmission_time_rtd = c(79.78819444, 
    57.59068053, 57.59068053, 57.59068053, 57.59068053, 9.893055556
    ), infection_flag = c(0L, 0L, 1L, 1L, 0L, 1L), temperature_value = c(98.9, 
    98.9, 98, 101.3, 99.5, 98.1), heartrate_value = c(106, 61, 
    78, 91, 120, 68), pH_result_time_rta = c(11, 85.50402145, 
    85.50402145, 85.50402145, 85.50402145, 85.50402145), gcst_value = c(15, 
    15, 15, 14.63769293, 15, 14.63769293)), row.names = c(NA, 
6L), class = "data.frame")

instead of manually typing name of columns which has categorical values, is there any way we can detect categorical columns and drop it?而不是手动输入具有分类值的列的名称,有什么方法可以检测分类列并将其删除?

I am concerning the case such as dataframe might have more than 10 categorical columns, it is sort of pain, so I am curious if it is possible using R.我关心的情况是 dataframe 可能有超过 10 个分类列,这有点痛苦,所以我很好奇是否可以使用 R。 any thought?任何想法?

for example, I can do this for above dataframe by manually looking at which one are categorical columns:例如,我可以通过手动查看哪一个是分类列来为上述 dataframe 执行此操作:

mydf <- mydf[!names(mydf) %in% c("gender", "infection_flag")]

is there any way we can detect which ones is categorical columns and drop it for numerical calculation purpose?有什么方法可以检测哪些是分类列并将其删除以进行数值计算? any idea?任何想法?

You can use dplyr and select all the numerical columns:您可以使用dplyr和 select 所有数值列:

library(dplyr)

mydf %>% select_if(is.numeric)

An option with base R带有base R的选项

i1 <- sapply(mydf, is.numeric)
df[i1]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何对 r 中的多个分类列值进行子集化? - How to subset multiple categorical column values in r? 如何在 R 中的值行上创建分类列条件? - How to create categorical column condition on values rows in R? 如何为具有多个分类值的单列将长格式转换为宽格式 - How to convert long to wide format for single column with multiple categorical values 如何检查一列的唯一值是否多次出现在 R 中另一列的不同值? - How can I check if unique values of a column has multiple occurrences for different values of another column in R? 如何从datetime列中提取小时,其中R中具有不同格式的值? - How do I extract hour from datetime column which has values in different formats in R? 如何根据 R 中的特定分类列值创建两个金额列 - How to Create Two Amount Columns Based on Specific Categorical Column Values in R 如何通过使用 plot() 通过分类列的值增加颜色的阴影来为 plot 着色 - How to color plot by increasing shade of a color by the values of a categorical column using plot() function R 如何在R数据框中的特定列中删除十进制值? - How can I drop decimal values in a specific column in R dataframe? 如何在 R 中创建一个新列来匹配来自两个不同数据框中的多个值 - How to create a new column in R which matches multiple values from two different data frames 在 R 中,如何删除其值全为 FALSE 的列? - In R, how do I drop a column whose values are all FALSE?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM