Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将文本转换为r中的行_R_Regex - Fatal编程技术网

将文本转换为r中的行

将文本转换为r中的行,r,regex,R,Regex,我有如下df,并希望将每个字母转换为行 df id text 1 ABC 2 EF 具有base R sp <- strsplit(data[,2], "") out <- as.data.frame(do.call(rbind,lapply(1:length(sp),function(x) cbind(data[x,1],sp[[x]])))) V1 V2 1 1 A 2 1 B 3 1 C 4 2 E 5 2 F 数据: dat

我有如下df,并希望将每个字母转换为行

df
id    text
1     ABC
2     EF

具有
base R

sp <- strsplit(data[,2], "")
out <- as.data.frame(do.call(rbind,lapply(1:length(sp),function(x) cbind(data[x,1],sp[[x]]))))

  V1 V2
1  1  A
2  1  B
3  1  C
4  2  E
5  2  F
数据:

data <- read.table(text="id    text
1     ABC
2     EF",header=T,stringsAsFactors=FALSE)
数据另一种方法:

library(tidyverse) 

df <- read.table(header = T, text = "id text
1 ABC
2 EF")

df %>%
    mutate(text = strsplit(as.character(text), "")) %>%
    unnest(text)


您还可以使用
stack
ie
stack(setNames(strsplit(data$text,“”),data$id))[2:1]
@Onyambu非常干净和好的解决方案!我认为你应该提供它作为一个答案。只是把它添加到你的上面
data <- read.table(text="id    text
1     ABC
2     EF",header=T,stringsAsFactors=FALSE)
library(tidyverse) 

df <- read.table(header = T, text = "id text
1 ABC
2 EF")

df %>%
    mutate(text = strsplit(as.character(text), "")) %>%
    unnest(text)

  id text
1  1    A
2  1    B
3  1    C
4  2    E
5  2    F