Consider the below hypothetical data:
x <- "There is a horror movie running in the iNox theater. : If row names are supplied of length one and the data
frame has a single row, the row.names is taken to specify the row names and not a column (by name or number).
If row names are supplied of length one and the data frame has a single row, the row.names is taken to specify
the row names and not a column (by name or number) Can we go : Please"
y <- "There is a horror movie running in the iNox theater. If row names are supplied of length one and the data
frame has a single row, the row.names is taken. To specify the row names and not a column. By name or number. :
If row names are supplied of length one and the data frame has a single row, the row.names is taken to specify
the row names and not a column (by name or number) Can we go : Please"
z <- "There is a horror movie running in the iNox theater. If row names are supplied of length one and the data frame has a single row, the row.names is taken to specify the row names and not a column (by name or number).
If row names are supplied of length one. : And the data frame has a single row, the row.names is taken to specify
the row names and not a column (by name or number) Can we go : Please"
df <- data.frame(Text = c(x, y, z), row.names = NULL, stringsAsFactors = F)
Did you notice that there is an ":" at different locations. For example:
- In 'x' it ( ":" ) is after the first sentence.
- In 'y' it ( ":" ) is after the fourth sentence.
- and In 'z' it is after the sixth sentence.
- Moreover there is one more ":" before the last sentence in the each text.
What I want to do, create two columns such that:
- Only the first ":" is considered and NOT THE LAST ONE.
- If there is a ":" within first three sentences, then divide the whole text into two columns otherwise, keep all the text in the second columns and 'NA' in the first column.
Wanted Output for 'x':
Col1 Col2
There is a horror movie running in the iNox theater. If row names are supplied of length one and the data frame has a single row, the row.names is taken to specify the row names and not a column (by name or number). If row names are supplied of length one and the data frame has a single row, the row.names is taken to specify the row names and not a column (by name or number) Can we go : Please
Wanted Output for 'y' (because ":" is not found within first three sentences, therefore) :
Col1 Col2
NA There is a horror movie running in the iNox theater. If row names are supplied of length one and the data frame has a single row, the row.names is taken. To specify the row names and not a column. By name or number. : If row names are supplied of length one and the data frame has a single row, the row.names is taken to specify the row names and not a column (by name or number) Can we go : Please
Just like above result for 'y', the Wanted Output result for 'z' should be:
Col1 Col2
NA all of the text from 'z'
What I am trying to do is:
resX <- data.frame(Col1 = gsub("\\s\\:.*$","\\1", df$Text[[1]]),
Col2 = gsub("^[^:]+(?:).\\s","\\1", df$Text[[1]]))
resY <- data.frame(Col1 = gsub("\\s\\:.*$","\\1", df$Text[[2]]),
Col2 = gsub("^[^:]+(?:).\\s","\\1", df$Text[[2]]))
resZ <- data.frame(Col1 = gsub("\\s\\:.*$","\\1", df$Text[[3]]),
Col2 = gsub("^[^:]+(?:).\\s","\\1", df$Text[[3]]))
And then merging above into a resulting dataframe "resDF" using rbind.
Issues are:
- The above can be done using "for() loop" or anyother method to make code simpler.
- The result from "y" and "z" text are not coming as I wanted (shown above).