2017-06-20 16 views
0

文字列から抽出する必要のあるさまざまなカテゴリの数を列に動的に追加するには、いくつかの助けが必要です。列の動的追加R

私のデータには、カテゴリの名前とその数を含む列があります。フィールドは空でも構わないし、考えられるカテゴリの任意の組み合わせを含んでいてもよい。

themes:firstcategory_1;secondcategory_33;thirdcategory_5 
themes:secondcategory_33;fourthcategory_2 
themes:fifthcategory_1 

私が必要とするのは、各カテゴリの列(カテゴリの名前を持つ必要があります)と上記の文字列から抽出された数です。カテゴリのリストは動的なので、どのカテゴリが存在するかは事前にわかりません。

私はこれにどのようにアプローチしますか?

+3

あなたが探しているもののより良いアイデアを得るために、希望する出力をテーブルとして含めてください。また、これを解決しようとしたコードと間違った場所を含めてください。 – lmo

答えて

0

このコードでは、各カテゴリの列が各行のカウントとともに取得されます。

library(dplyr) 
library(tidyr) 
library(stringr) 

# Create test dataframe 
df <- data.frame(themes = c("firstcategory_1;secondcategory_33;thirdcategory_5", "secondcategory_33;fourthcategory_2","fifthcategory_1"), stringsAsFactors = FALSE) 

# Get the number of columns to split values into 
cols <- max(str_count(df$themes,";")) + 1 

# Get vector of temporary column names 
cols <- paste0("col",c(1:cols)) 

df <- df %>% 
     # Add an ID column based on row number 
     mutate(ID = row_number()) %>% 
     # Separate multiple categories by semicolon 
     separate(col = themes, into = cols, sep = ";", fill = "right") %>% 
     # Gather categories into a single column 
     gather_("Column", "Value", cols) %>% 
     # Drop temporary column 
     select(-Column) %>% 
     # Filter out NA values 
     filter(!is.na(Value)) %>% 
     # Separate categories from their counts by underscore 
     separate(col = Value, into = c("Category","Count"), sep = "_", fill = "right") %>% 
     # Spread categories to create a column for each category, with the count for each ID in that category 
     spread(Category, Count) 
関連する問題