maxie maxie - 1 month ago 6
R Question

data.frame in R from text

I have a text (from a text file with one line), something like

hello world
.

I want to convert this text in a data.frame that looks like this.

V1 V2
1 h 0
1 e 1
1 l 0
1 l 0
1 o 1
1 <SPACE> NA
1 w 0
1 o 1
1 r 0
1 l 0
1 d 0


All vals should have value 1, all other letters value 0 and all should be NA.

How can I achieve this?

Answer

You could use strsplit to split your character string and grepl to find the vowels:

mystring <- "hello world"
df <- data.frame(V1 = unlist(strsplit(mystring, "")))
df$V2 <- as.numeric(grepl("[aeiou]", df$V1))

>df
#   V1 V2
#1   h  0
#2   e  1
#3   l  0
#4   l  0
#5   o  1
#6      0
#7   w  0
#8   o  1
#9   r  0
#10  l  0
#11  d  0

If you want to replace 0 by NA for empty cells in V1, you could use ifelse:

df$V2 <- ifelse(df$V1 == " ", NA, df$V2)