2016-09-27 1 views
0

у меня есть dataframe, которые содержат:в R, как получить строки, содержащие значения в списке и создать dataframe подсчетов

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 

Я создал векторное представление столбца Содержание и нового 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") 

Но я озадачен в тр ying, чтобы вернуть частотное распределение количества типов блюд, содержащих содержание Top N?

Могу ли я запустить цикл, используя top2_v1 в df2, поэтому я могу создать еще один фреймворк, который позволит мне узнать частоту для каждого содержимого Top N?

ответ

0

Попробуйте это (начиная с 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 
+0

Im извините, если я не был ясен. Я уже смог получить top2_v1. То, что я был в тупике, - это получить граф типа питания, который содержит top2_v1, используя df2 dataframe. –

+0

Тогда каково ваше требование? получить частотные распределения? обновил вышеуказанный код. –

+0

О, я вижу, пожалуйста, снова найдите обновленный код, –

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])))) 
table(df2[indices,]$Meal) 
table(df2[indices,]$Meal)/nrow(df[indices,]) 
barplot(sort(table(df2[indices,]$Meal)/nrow(df[indices,]), decreasing = TRUE), 
                   ylab='Proportions') 

enter image description here