I am splitting a column in a dataset by using strsplit and wish to map one column to the split data.
Here is a sample dataset:
https://drive.google.com/file/d/1jtrn6Htezz6iRhJN0HaxXowT5JZW52ai/view?usp=sharing
My code is as follows:
library(readr)
df <- read_csv("sample for community.csv", col_names = FALSE)[,1:2]
x<-strsplit(df$X2, '\n')
y5<-x[lapply(x, length) ==5]
y4<-x[lapply(x, length) ==4]
y3<-x[lapply(x, length) ==3]
p5<-data.frame(unlist(lapply(y5, `[[`, 1)),unlist(lapply(y5, `[[`, 2)),unlist(lapply(y5, `[[`, 3)),unlist(lapply(y5, `[[`, 4)),unlist(lapply(y5, `[[`, 5)))
p4<-data.frame(unlist(lapply(y4, `[[`, 1)),unlist(lapply(y4, `[[`, 2)),unlist(lapply(y4, `[[`, 3)),unlist(lapply(y4, `[[`, 4)))
p3<-data.frame(unlist(lapply(y3, `[[`, 1)),unlist(lapply(y3, `[[`, 2)),unlist(lapply(y3, `[[`, 3)))
p5[,5]<-NULL
p3[,4]<-rep("NA")
colnames(p5)<-c("X1","X2","X3","X4")
colnames(p4)<-c("X1","X2","X3","X4")
colnames(p3)<-c("X1","X2","X3","X4")
final<-rbind(p5,p4,p3)
As you can see, the order of the rows change due to some data having a different number of lines.
I wish to merge the first column onto the final dataset but cannot work out how to do so.
In the real dataset it will not be possible to match by matching strings (E.g. Match "String1" with columns containing "String1")
All help is highly appreciated.
Thanks,
Matt