简体   繁体   English

通过 R 中的数字列选择数据框

[英]Dataframe selection by numeric columns in R

This is very easy to do in python but is tripping me up in R.这在 python 中很容易做到,但在 R 中让我绊倒。

numeric_cols<-data_all %>% select_if(is.numeric)
columns <-colnames(numeric_cols)
data_all[colnames] # returns dataframe selection

data_all[which(rowSums(data_all[colnames]) > 300),]

Giving the error:给出错误:

Warning message in cbind(parts$left, ellip_h, parts$right, deparse.level = 0L):
“number of rows of result is not a multiple of vector length (arg 2)


rowSums(data_wideALL[colnames] > 300)

Returns退货

<NA><NA><NA><NA><NA><NA><NA><NA><NA><NA><NA><NA><NA><NA><NA><NA><NA><NA><NA><NA><NA><NA><NA><NA><NA><NA><NA><NA><NA><NA><NA><NA><NA><NA>

How do I approach this in R我如何在 R 中解决这个问题

try this:尝试这个:

numeric_cols <- data_all %>%
    select_if(is.numeric)

num_cols <- names(numeric_cols)

data_all <- data_all %>%
    select(num_cols) 

data_all$row_sum <- rowSums(data_all)

data_all <- data_all %>%
    filter(row_sum > 300)

It is a bit difficult to answer your question without knowing the exact question requirements and reproducible code.在不知道确切的问题要求和可重现代码的情况下回答您的问题有点困难。 Is this what you after ?这是你追求的吗?

numeric_cols<-data_all %>% select_if(is.numeric)
columns <-colnames(numeric_cols)

data_all<-data_all[columns] # returns dataframe selection

data_all[rowSums(data_all[columns] > 300),]

You can use sapply with is.numeric like this in base R:您可以使用sapplyis.numeric像这样的基础R:

# assign a data set
dat <- data.frame(A = c(1L, 2L, 3L), B = c(TRUE, TRUE, FALSE), 
                  C = c(1, 2, 3), D = c(50, 350, 700))

# use sapply + is.numeric
dat[sapply(dat, is.numeric)]
#R>   A C   D
#R> 1 1 1  50
#R> 2 2 2 350
#R> 3 3 3 700

Then you can do something like this if you only want the rows which has a sum which is greater than 300:然后,如果您只想要总和大于 300 的行,则可以执行以下操作:

dat[rowSums(dat[sapply(dat, is.numeric)]) > 300, ]
#R>   A     B C   D
#R> 2 2  TRUE 2 350
#R> 3 3 FALSE 3 700

A solution without the non-numeric columns is:没有非数字列的解决方案是:

dat <- dat[sapply(dat, is.numeric)]
dat[rowSums(dat) > 300, ]
#R>   A C   D
#R> 2 2 2 350
#R> 3 3 3 700

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

相关问题 按R中的数值向量对Dataframe列进行排序 - Sort Dataframe columns by numeric vector in R R 变异选择 dataframe 列使用另一个 dataframe 具有相同名称的列选择 - R mutate selection of dataframe columns using another dataframe with same named selection of columns R-将数据框中所有列的数据类型从字符动态转换为数字 - R - convert datatype of all columns in a dataframe from character to numeric dynamically 将数字数据框拆分为R中2列的所有可能组合 - Split a numeric dataframe into all possible combinations of 2 columns in R 使用 R 和 dplyr 的相关控制组对 dataframe 中的所有数字列进行规范化 - Normalise all numeric columns in a dataframe by relevant control group with R and dplyr 将具有巨大字符列的数据框重塑为 R 中的多个数字列 - Reshaping a dataframe with a huge character column into several numeric columns in R r-通过数据框中的数值过滤字母数字因子列 - r - filtering alphanumeric factor columns by numeric values in dataframe R - 将 dataframe 列转换为数字 - 强制错误引入的 NA - R - convert dataframe columns to numeric - NAs introduced by coercion error 如何将 R dataframe 中的列子集中的数值更改为其他数值? - How do I change numeric values in a subset of columns in a R dataframe to other numeric values? 标准化 R 中的数字列 - Normalizing Numeric Columns in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM