如何在R中把句子分成两半

如何在R中把句子分成两半,r,string,R,String,我有一个字符串向量,我希望每个字符串在最近的空间被大致切成两半 例如,使用以下数据: test <- data.frame(init = c("qsdf mqsldkfop mqsdfmlk lksdfp pqpdfm mqsdfmj mlk", "qsdf", "mp mlksdfm mkmlklkjjjjjjjjjjjjjjjjjjjjjjklmmjlkjll", "qsddddddddddddddddddddddddddddddd",

我有一个字符串向量,我希望每个字符串在最近的空间被大致切成两半

例如,使用以下数据:

test <- data.frame(init = c("qsdf mqsldkfop mqsdfmlk lksdfp pqpdfm mqsdfmj mlk",
      "qsdf",
      "mp mlksdfm mkmlklkjjjjjjjjjjjjjjjjjjjjjjklmmjlkjll",
      "qsddddddddddddddddddddddddddddddd",
      "qsdfmlk mlk mkljlmkjlmkjml lmj mjjmjmjm lkj"), stringsAsFactors = FALSE)

任何不分两半,但“使第一部分长度不超过X个字符”的解决方案也很好

首先,我们用空格分割字符串

a <- strsplit(test$init, " ")
最后,我们
rbind
组合字符串并更改列名

newdf <- do.call(rbind.data.frame, combined)
names(newdf) <- c("first", "second")

newdf首先,我们用空格分割字符串

a <- strsplit(test$init, " ")
最后,我们
rbind
组合字符串并更改列名

newdf <- do.call(rbind.data.frame, combined)
names(newdf) <- c("first", "second")

newdf您可以使用我编写的软件包中的函数
nbbreak

devtools::install_github("igorkf/breaker")
library(tidyverse)

test <- data.frame(init = c("Phrase with four words", "That phrase has five words"), stringsAsFactors = F)

#This counts the numbers of words of each row:
nwords = str_count(test$init, " ") + 1

#This is the position where break the line for each row:
break_here = ifelse(nwords %% 2 == 0, nwords/2, round(nwords/2) + 1)

test
#                        init
# 1     Phrase with four words
# 2 That phrase has five words

#the map2_chr is applying a function with two arguments,
#the string is "init" and the n is "break_here":
test %>%
  mutate(init = map2_chr(init, break_here, ~breaker::nbreak(string = .x, n = .y, loop = F))) %>%
  separate(init, c("first", "second"), sep = "\n")
#             first     second
# 1     Phrase with four words
# 2 That phrase has five words
devtools::安装github(“igorkf/breaker”)
图书馆(tidyverse)
测试%
突变(init=map2_chr(init,break_在这里,~break::nbbreak(string=.x,n=.y,loop=F)))%>%
单独(初始,c(“第一”,“第二”),sep=“\n”)
#第一秒
#一个短语加四个单词
#那个短语有五个词

您可以使用我编写的软件包中的函数
nbbreak

devtools::install_github("igorkf/breaker")
library(tidyverse)

test <- data.frame(init = c("Phrase with four words", "That phrase has five words"), stringsAsFactors = F)

#This counts the numbers of words of each row:
nwords = str_count(test$init, " ") + 1

#This is the position where break the line for each row:
break_here = ifelse(nwords %% 2 == 0, nwords/2, round(nwords/2) + 1)

test
#                        init
# 1     Phrase with four words
# 2 That phrase has five words

#the map2_chr is applying a function with two arguments,
#the string is "init" and the n is "break_here":
test %>%
  mutate(init = map2_chr(init, break_here, ~breaker::nbreak(string = .x, n = .y, loop = F))) %>%
  separate(init, c("first", "second"), sep = "\n")
#             first     second
# 1     Phrase with four words
# 2 That phrase has five words
devtools::安装github(“igorkf/breaker”)
图书馆(tidyverse)
测试%
突变(init=map2_chr(init,break_在这里,~break::nbbreak(string=.x,n=.y,loop=F)))%>%
单独(初始,c(“第一”,“第二”),sep=“\n”)
#第一秒
#一个短语加四个单词
#那个短语有五个词

你是对的,我更正了示例。你是对的,我更正了示例。
devtools::install_github("igorkf/breaker")
library(tidyverse)

test <- data.frame(init = c("Phrase with four words", "That phrase has five words"), stringsAsFactors = F)

#This counts the numbers of words of each row:
nwords = str_count(test$init, " ") + 1

#This is the position where break the line for each row:
break_here = ifelse(nwords %% 2 == 0, nwords/2, round(nwords/2) + 1)

test
#                        init
# 1     Phrase with four words
# 2 That phrase has five words

#the map2_chr is applying a function with two arguments,
#the string is "init" and the n is "break_here":
test %>%
  mutate(init = map2_chr(init, break_here, ~breaker::nbreak(string = .x, n = .y, loop = F))) %>%
  separate(init, c("first", "second"), sep = "\n")
#             first     second
# 1     Phrase with four words
# 2 That phrase has five words