簡體   English   中英

R中如何獲取包含列表中值的行並創建計數數據框

[英]in R how to get rows that contain values in a list and create a dataframe of counts

我有一個包含以下內容的數據框:

   Meal        Contents     
   Type_1      redberries,strawberry,blackberry
   Type_2      banana,apple,strawberry,
   Type_3      rice,chicken
   Type_4      beef,stringbeans,mashpotatoes
   Type_5      banana,strawberry,berry,cantaloupe

我創建了Contents列的矢量表示,新的df2是

 Meal           Contents                          Strawberry   Banana   Rice
   Type_1      redberries,strawberry,blackberry     1            0      0
   Type_2      banana,apple,strawberry,             1            1    
   Type_3      rice,chicken                         0            0
   Type_4      beef,stringbeans,mashpotatoes        0            0
   Type_5      banana,strawberry,berry,cantaloupe   1            1

我試圖根據計數獲得前2個內容:

  top2_v1 <- c("strawberry","banana")

但是我很想嘗試獲取包含前N個含量的膳食類型計數的頻率分布???

我可以使用df2數據幀中的top2_v1運行循環,以便創建另一個數據幀,讓我知道每個前N個內容的頻率嗎?

試試看(從df2開始):

df2

    Meal                           Contents apple banana beef berry blackberry cantaloupe chicken mashpotatoes redberries rice strawberry stringbeans
1 Type_1   redberries,strawberry,blackberry     0      0    0     0          1          0       0            0          1    0          1           0
2 Type_2           banana,apple,strawberry,     1      1    0     0          0          0       0            0          0    0          1           0
3 Type_3                       rice,chicken     0      0    0     0          0          0       1            0          0    1          0           0
4 Type_4      beef,stringbeans,mashpotatoes     0      0    1     0          0          0       0            1          0    0          0           1
5 Type_5 banana,strawberry,berry,cantaloupe     0      1    0     1          0          1       0            0          0    0          1           0

n <- 2
topn_v1  <- names(sort(colSums(df2[3:ncol(df2)]), decreasing=TRUE))[1:n]
indices <- apply(df2, 1, function(x) any(as.integer(as.character(x[topn_v1]))))

df2[indices,] # Meals that contain at least one of the top_n Contents
    Meal                           Contents apple banana beef berry blackberry cantaloupe chicken mashpotatoes redberries rice strawberry stringbeans
1 Type_1   redberries,strawberry,blackberry     0      0    0     0          1          0       0            0          1    0          1           0
2 Type_2           banana,apple,strawberry,     1      1    0     0          0          0       0            0          0    0          1           0
5 Type_5 banana,strawberry,berry,cantaloupe     0      1    0     1          0          1       0            0          0    0          1           0

table(df2[indices,]$Meal)   

Type_1 Type_2 Type_3 Type_4 Type_5 
 1      1      0      0      1 

table(df2[indices,]$Meal) / nrow(df[indices,]) # in proportion

   Type_1    Type_2    Type_3    Type_4    Type_5 
0.3333333 0.3333333 0.0000000 0.0000000 0.3333333 

嘗試這個:

 n <- 2
 topn_v1  <- names(sort(colSums(df2[3:ncol(df2)]), decreasing=TRUE))[1:n]
 indices <- apply(df2, 1, function(x) any(as.integer(as.character(x[topn_v1]))))
 table(df2[indices,]$Meal)
 table(df2[indices,]$Meal) / nrow(df[indices,])
 barplot(sort(table(df2[indices,]$Meal) / nrow(df[indices,]), decreasing = TRUE), 
                                                              ylab='Proportions')

在此處輸入圖片說明

暫無
暫無

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

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