需要对数据集执行n-gram

时间:2018-09-22 05:08:07

标签: r

我需要对以下数据集执行n-gram。

Column 1

Electronic, socket, clamp, cooler

Actuator, cylinder, valve

Hydraulic, electronic, cooler

Actuator, Pnematic, cylinder, valve

我需要如下输出,

Column 1

Electronic_socket, socket_clamp, clamp_cooler

Actuator_cylinder, cylinder_valve

Hydraulic_electronic, electronic_cooler

Actuator_Pneumatic, Pneumatic_cylinder, cylinder_valve

1 个答案:

答案 0 :(得分:0)

以一个示例为例,假设您的数据位于data.frame的column_1中,则以下代码应使用基数R创建您的n-gram:

# build original data as a data.frame with 1 column
df <- data.frame( column_1 = 
                     c("Electronic, socket, clamp, cooler",
                       "Actuator, cylinder, valve",
                       "Hydraulic, electronic, cooler",
                       "Actuator, Pnematic, cylinder, valve"), 
                  stringsAsFactors=FALSE)

 lov <- strsplit(df$column_1, ', ', fixed=TRUE)

 sapply(lov, function(x){paste0(x[ -length(x) ], '_', x[-1])})

结果:

[[1]]
[1] "Electronic_socket" "socket_clamp"      "clamp_cooler"     

[[2]]
[1] "Actuator_cylinder" "cylinder_valve"   

[[3]]
[1] "Hydraulic_electronic" "electronic_cooler"   

[[4]]
[1] "Actuator_Pnematic" "Pnematic_cylinder" "cylinder_valve"   

解释此解决方案:

考虑一次只处理一行内容,例如 如果向量中已经有一行中的单词:

    x <- c("Electronic", "socket", "clamp", "cooler")

您所需的输出是:Electronic_socket, socket_clamp, clamp_cooler

x[ -length(x) ]  #gives you all but the last word
# [1] "Electronic" "socket"     "clamp"     

x[-1]  #gives you everything except the first word
# [1] "socket" "clamp"  "cooler"

#paste them together with a "_" between yields your desired output for that line
paste0( x[ -length(x) ], '_', x[-1] )
# [1] "Electronic_socket" "socket_clamp"      "clamp_cooler"