如何查詢R資料框中滿足基於行值條件的列數?
有時我們希望從資料框中提取計數,並且該計數可能是基於行值具有相同特徵的列數。例如,如果我們有一個包含三列五十行的數 據框,並且值是 1 到 100 之間的整數,那麼我們可能希望查詢每行中值大於 20 的列數。這可以透過使用 rowSums 函式來完成。
示例
考慮以下資料框 -
> x1<-sample(1:10,20,replace=TRUE) > x2<-sample(1:100,20) > x3<-rpois(20,5) > df<-data.frame(x1,x2,x3) > df
輸出
x1 x2 x3 1 9 72 9 2 5 20 6 3 3 82 4 4 5 47 4 5 1 45 10 6 6 14 6 7 10 54 7 8 10 13 6 9 4 98 5 10 4 76 5 11 5 53 5 12 9 87 2 13 3 79 6 14 2 73 5 15 10 75 3 16 1 7 2 17 5 92 7 18 5 34 5 19 9 52 5 20 5 43 4
向 df 新增一個新列,其中包含值大於 5 的列數 -
示例
> df$Number_of_columns_LargerThan5<-rowSums(df>5) > df
輸出
x1 x2 x3 Number_of_columns_LargerThan5 1 9 72 9 3 2 5 20 6 2 3 3 82 4 1 4 5 47 4 1 5 1 45 10 2 6 6 14 6 3 7 10 54 7 3 8 10 13 6 3 9 4 98 5 1 10 4 76 5 1 11 5 53 5 1 12 9 87 2 2 13 3 79 6 2 14 2 73 5 1 15 10 75 3 2 16 1 7 2 1 17 5 92 7 2 18 5 34 5 1 19 9 52 5 2 20 5 43 4 1
向 df 新增一個新列,其中包含值小於 5 的列數 -
示例
> df$Number_of_columns_LessThan5<-rowSums(df<5) > df
輸出
x1 x2 x3 Number_of_columns_LargerThan5 Number_of_columns_LessThan5 1 9 72 9 3 1 2 5 20 6 2 1 3 3 82 4 1 3 4 5 47 4 1 2 5 1 45 10 2 2 6 6 14 6 3 1 7 10 54 7 3 1 8 10 13 6 3 1 9 4 98 5 1 2 10 4 76 5 1 2 11 5 53 5 1 1 12 9 87 2 2 2 13 3 79 6 2 2 14 2 73 5 1 2 15 10 75 3 2 2 16 1 7 2 1 3 17 5 92 7 2 1 18 5 34 5 1 1 19 9 52 5 2 1 20 5 43 4 1 2
讓我們看看另一個例子 -
示例
> y1<-sample(1:100,20) > y2<-sample(1:1000,20) > df_y<-data.frame(y1,y2) > df_y
輸出
y1 y2 1 33 663 2 20 523 3 24 791 4 100 330 5 48 264 6 32 579 7 56 51 8 94 57 9 76 711 10 58 411 11 49 849 12 63 805 13 67 696 14 1 237 15 11 147 16 12 448 17 75 465 18 65 220 19 99 958 20 34 909
> df_y$Number_of_columns_less_than_equalto_50<-rowSums(df_y<=50) > df_y
輸出
y1 y2 Number_of_columns_less_than_equalto_50 1 33 663 1 2 20 523 1 3 24 791 1 4 100 330 0 5 48 264 1 6 32 579 1 7 56 51 0 8 94 57 0 9 76 711 0 10 58 411 0 11 49 849 1 12 63 805 0 13 67 696 0 14 1 237 1 15 11 147 1 16 12 448 1 17 75 465 0 18 65 220 0 19 99 958 0 20 34 909 1
廣告