I have the following data frame. Using the stringr package, I've attempted
to map the url's to some specific elements that are in each url. I then
used the reshape package to join two different data frames. The next step
is to transform the two columns in the mydt data frame (forester and
customer_support) into one column which specified whether or not it
contained that element or not (0 or 1).


url = data.frame(u=c("http://www.subaru.com/vehicles/impreza/index.html";,
        "
http://www.subaru.com/index.html?s_kwcid=subaru&k_clickid=214495e6-dbe0-6668-9222-00003d7cd876&prid=87&k_affcode=76602
",
        "http://www.subaru.com/customer-support.html";,
        "http://www.subaru.com/";,
        "http://www.subaru.com/vehicles/forester/index.html";))
url

cs = c("customer-support")
f = c("forester")
one_match <- str_c(cs, collapse = "|")
two_match <- str_c(f, collapse = "|")
main <- function(df) {
  df$customer_support <- as.numeric(str_detect(url$u, one_match))
  df
}
d1 = main(url)
d1
main <- function(df) {
  df$forester <- as.numeric(str_detect(url$u, two_match))
  df
}
d2 = main(url)
d2
mydt = join(d1, d2)
str(mydt)
reshape(mydt, direction="long", idvar="u", varying=2:3, sep="")
reshape(mydt, varying=2:3, direction ="long",  idvar = "u")



I've messed around with the reshape package but I'm not able to figure it
out. Is there an alternative package or function I can use to get the
desired result.

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to