sorry for the very unspecific title. I really don't know how to name the problem... So I'm dealing with this data frame :
C1 <- c("made in Italy", "100% silk", "dry clean only")
C2 <- c("80% cotton, 20% polyester","made in France", "hand wash")
C3 <- c("made in Italy", "Designer color : vanilla", " 100% nylon")
eg <- as.data.frame(rbind(C1,C2,C3))
I'd like to add one veriable "composition" by extracting all the values containing "%" sign. As you can see, the composition values are not in the same column for each observation... I tried several methods but failed to achieve this. for example :
fcompo <- function(x){
if (grepl('%',x) = TRUE){eg$composition <- paste(x)}
else {eg$composition=NA}
}
then I got lost in the function... being very new to R language... I also tried :
library(stringr)
eg$composition <- str_extract(eg[,c(1:3)], "%$" )
thanks a lot for your generous help
Aucun commentaire:
Enregistrer un commentaire