簡體   English   中英

計算 dataframe 中每列中指定特定條件的行數

[英]Count number of rows in each column in a dataframe that specify a specific condition

R 的新手順便說一句,如果這看起來像一個愚蠢的問題,我很抱歉。 所以基本上我有一個 dataframe 有 100 行和 3 個不同的數據列。 我還有一個帶有 3 個閾值的向量,每列一個。 我想知道如何過濾掉每列中優於每個閾值的值。

編輯:對不起不完整的問題。 So essentially what i would like to create is a function (that takes a dataframe and a vector of tresholds as parameters) that applies every treshold to their respective column of the dataframe (so there is one treshhold for every column of the dataframe). 每列中“尊重”其閾值的元素數量稍后應放入向量中。 例如:

第 1 列:值 = 1、2、3。 閾值 =(僅限低於 3 的值)第 2 列:值 = 4、5、6。 閾值 =(僅低於 6 的值) Output:向量 (2,2),因為每列中有兩個元素位於各自的閾值之下。

謝謝大家的幫助!!

您的示例數據:

df <- data.frame(a = 1:3, b = 4:6)
threshold <- c(3, 6)

解決您的問題的一種選擇是使用sapply() ,它將 function 應用於列表或向量。 在這種情況下,我使用1:ncol(df)df中的列創建一個向量。 在 function 內部,您可以通過求和 TRUE 案例的數量來計算小於給定閾值的值的數量:

col_num <- 1:ncol(df)
sapply(col_num, function(x) {sum(df[, x] < threshold[x])})

或者,在一行中:

sapply(1:ncol(df), function(x) {sum(df[, x] < threshold[x])})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM