1
votes

I want to convert a column of factor into lists within a data.frame.
I made it with the code below, but I'm feeling this is not the right way.
How can I improve the code below ?

The data I'm dealing with is a result of association rules.(Using the package: arules) (it's in Japanese)
Here are 3 rows of the column "rules":

rules
{道路構造=交差点_交差点付近,昼間12時間平均旅行速度=20~30km/h,歩道設置率=100%,バス優先.専用レーンの有無=なし} => {事故類型=車両相互_追突}
{道路構造=交差点_交差点付近,昼間12時間平均旅行速度=20~30km/h,バス優先.専用レーンの有無=なし} => {事故類型=車両相互_追突}
{道路構造=交差点_交差点付近,歩道設置率=100%,バス優先.専用レーンの有無=なし,代表沿道状況=人口集中地区(商業地域を除く)} => {事故類型=車両相互_追突}

And str(data)

'data.frame': 50 obs. of 5 variables:
$ rules : Factor w/ 50 levels "{道路構造=交差点_交差点付近,バス優先.専用レーンの有無=なし,指定最高速度=50} => {事故類型=車両相互_追突}",..: 9 8 35 38 10 31 11 25 3 7 ...
$ support : Factor w/ 48 levels "0.050295052",..: 5 14 5 10 24 1 30 13 15 18 ...
$ confidence: Factor w/ 50 levels "0.555131629",..: 50 49 48 47 46 45 44 43 42 41 ...
$ lift : Factor w/ 50 levels "1.894879112",..: 50 49 48 47 46 45 44 43 42 41 ...
$ count : Factor w/ 48 levels "1013","1250",..: 9 18 9 14 28 5 34 17 19 22 ...

# convert factor to character
data %>% mutate_if(is.factor, as.character) -> data

# delete the RHS in rules(the part after '=>' )
data$rules <- strsplit(data$rules, " =>")
i = 1
for (i in 1:length(data$rules)) {
  data$rules[[i]] <- data$rules[[i]][[-2]]
}

# delete "{" and "}"
data$rules <- as.character(data$rules)
data$rules <- strsplit(data$rules, "[{]")
i = 1
for (i in 1:length(data$rules)) {
  data$rules[[i]] <- data$rules[[i]][[-1]]
}

data$rules <- as.character(data$rules)
data$rules <- strsplit(data$rules, "[}]")

# split character to list (:length(data$rules[[1]] -> 4))
data$rules <- as.character(data$rules)
data$rules <- strsplit(data$rules, ",")

The output should be like this:

[[1]]
[1] "道路構造=交差点_交差点付近"        "昼間12時間平均旅行速度=20~30km/h" "歩道設置率=100%"                   "バス優先.専用レーンの有無=なし"   

[[2]]
[1] "道路構造=交差点_交差点付近"        "昼間12時間平均旅行速度=20~30km/h" "バス優先.専用レーンの有無=なし"   

[[3]]
[1] "道路構造=交差点_交差点付近"                  "歩道設置率=100%"                             "バス優先.専用レーンの有無=なし"             
[4] "代表沿道状況=人口集中地区(商業地域を除く)"

My code did work, however, I just feel it's not beautiful, or efficient.
So could you improve it. Or, the right way to do this work.

1

1 Answers

0
votes

We can use str_extract

library(stringr)
library(dplyr)
out <- data %>% 
         mutate(rules = trimws(str_extract(rules, "(?<=\\{)[^}]+")))
out$rules
#[1] "道路構造=交差点_交差点付近,昼間12時間平均旅行速度=20~30km/h,歩道設置率=100%,バス優先.専用レーンの有無=なし"          
#[2] "道路構造=交差点_交差点付近,昼間12時間平均旅行速度=20~30km/h,バス優先.専用レーンの有無=なし"                          
#[3] "道路構造=交差点_交差点付近,歩道設置率=100%,バス優先.専用レーンの有無=なし,代表沿道状況=人口集中地区(商業地域を除く)"

If we want to split the 'rules' by , and create a list column

out$rules <- str_split(out$rules, ",")

data

data <- structure(list(rules = c("{道路構造=交差点_交差点付近,昼間12時間平均旅行速度=20~30km/h,歩道設置率=100%,バス優先.専用レーンの有無=なし} => {事故類型=車両相互_追突}", 
"{道路構造=交差点_交差点付近,昼間12時間平均旅行速度=20~30km/h,バス優先.専用レーンの有無=なし} => {事故類型=車両相互_追突}", 
"{道路構造=交差点_交差点付近,歩道設置率=100%,バス優先.専用レーンの有無=なし,代表沿道状況=人口集中地区(商業地域を除く)} => {事故類型=車両相互_追突}"
)), class = "data.frame", row.names = c(NA, -3L))