dataframe - R apply function on data frame column -
i need , efficiently, parse 1 of dataframe column (a url string) , call function (strsplit) parse it, e.g.:
url <- c("www.google.com/nir1/nir2/nir3/index.asp") unlist(strsplit(url,"/"))
my data frame : spark.data.url.clean looks this:
classes url [107,662,685,508,111,654,509] drudgereport.com/level1/level2/level3
this df has 100k rows , don't want loop/iterate on it, parse each url separately , write results new data frame. need/want create new 5 column data frame:
df.result <- data.frame(fullurl = as.character(),baseurl=as.character(), firstlevel = as.character(), secondlevel=as.character(),thirdlevel=as.character(),classificaiton=as.character())
call 1 of "apply" family function on spark.data.url.clean$url
, write results new data frame df.result
such first column (fullurl
) populated relevant spark.data.url.clean$url
, 2nd 5th columns populated relevant results applying
unlist(strsplit(url,"/"))
- taking first, 2nd, 3rd , 4th elements resulted vector , putting in first,2nd, 3rd , 4th columns in df.result
, putting spark.data.url.clean$classes
in new data frame columns df.result$classificaiton
sorry complication , let me know if need further cleared out.
there no need apply
, far see it.
try this:
spark.data.url.clean <- data.frame(classes = c(107,662,685,508,111,654,509), url = c("drudgereport.com/level1/level2/level3", "drudgeddddreport.com/levelfe1/lefvel2/leveel3", "drudgeaasreport2.com/lefvel13/lffvel244/fel223", "otherurl.com/level1/second/level3", "whateversite.com/level13/level244/level223", "esportsnow.com/first/level2/level3", "reeport2.com/level13/level244/third"), stringsasfactors = false) df.result <- spark.data.url.clean names(df.result) <- c("classification", "fullurl") df.result[c("baseurl", "firstlevel", "secondlevel", "thirdlevel")] <- do.call(rbind, strsplit(df.result$fullurl, "/"))
Comments
Post a Comment