I want to split a column based on another. I explain in the following.
here is part of my data:
brand products
APPLE IPHONE6SPlus_16G
APPLE IPHONE6S_64G
APPLE IPHONE6S_16G
APPLE IPhone6_32G
APPLE iPadAir2_64G
APPLE iPadmini2_16G
APPLE iPadmini4_64G
HTC ONEX
Samsung SamsungGalaxy
I want to split brand based on Products. here is what I actually want.
brand products
iPhone6S IPHONE6SPlus_16G
iPhone6S IPHONE6S_64G
iPhone6S IPHONE6S_16G
iPhone6 IPhone6_32G
APPLE iPadAir2_64G
APPLE iPadmini2_16G
APPLE iPadmini4_64G
HTC ONEX
Samsung SamsungGalaxy
I just want to split APPLE into three new(APPLE, iPhone6S, iPhone6) based on products. If the name in products contains IPHONE6SPlus, IPHONE6S, change brand to iPhone6S. If the name in products contains IPhone6, change brand to iPhone6. And the remainings do not change.
I think I can use iflese to do, but there are size(i.e. 16G, 64G, etc.) in products name.
How can I ignore these size and split the data.
greplto match the pattern i.e.df1$brand[grepl("IPHONE6S", df1$products)] <- "IPHONE6S"v1 <- sub("^(.)(.)(.{5})(.).*", "\\L\\1\\U\\2\\L\\3\\U\\4", df1$products, perl = TRUE);df1$brand[v1=="iPhone6S"] <- v1[v1 == "iPhone6S"]v1 <- sub("^(.)(.)(.{5})(.).*", "\\L\\1\\U\\2\\L\\3\\U\\4"", df1$products, perl = TRUE). Thanks