in my dataframe I have a column with long string like this
df$string
AVDSFBKLDF
What I need is to separate each character and create new column for it. Name of the columns is q1, q2 and so on. For similar cases like spliting string by symbol and put it into the new columns I used this code
df %>% separate(string, into = paste0('q', 1:10), sep = "")
It worked fine but now when I want to split string by each characters I'm getting some blank warning my console and my code do not works.
The data.table
package provides the function tstrsplit
that you might consider.
a<- c("AVDSFBKLDF", "GH", "ABCD")
library(data.table)
DT <- data.table(a)
DT_wide <- DT[, tstrsplit(a, "")]
# change column names
setnames(DT_wide, paste0("q", seq_len(ncol(DT_wide))))
DT_wide
# q1 q2 q3 q4 q5 q6 q7 q8 q9 q10
#1: A V D S F B K L D F
#2: G H <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
#3: A B C D <NA> <NA> <NA> <NA> <NA> <NA>
If you want to continue to work with a dataframe, type setDF(DT_wide)
at the end.
Use the below code. i have used stringr package str_split_fixed function to create new columns.
a<- c("AVDSFBKLDF")
library(stringr)
d<- data.frame(str_split_fixed(a, "", max(nchar(a))))
I hope this helps
You could try :
df<- data.frame(string=c("ABCDEF","GH"))
df %>% mutate(v=str_split(string,"(?=.)")) %>% unnest %>%
filter(v!="") %>%
group_by(string) %>% mutate(k=paste0("q",row_number())) %>% ungroup %>%
spread(k,v)
## A tibble: 2 x 7
# string q1 q2 q3 q4 q5 q6
# <fct> <chr> <chr> <chr> <chr> <chr> <chr>
#1 ABCDEF A B C D E F
#2 GH G H <NA> <NA> <NA> <NA>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.