r - apply a function to a subset of a data frame but retain the whole data frame -
i have data frame , vector identifying rows i'm interested in:
col1 <- c("june","june","june 11","june 11, 2012","june 14, 2012") col2 <- c("september", "september", "october 8", "october", "sept 27, 2012") monthdf <- data.frame(cbind(col1, col2), stringsasfactors = false) v0 <- c(1, 2)
i have 2 character vectors apply subset , specific columns.
startmonthv <- paste(c(" 1, ", substr(sys.date(), 1, 4)), collapse = "") endmonthv <- paste(c(" 28, ", substr(sys.date(), 1, 4)), collapse = "")
i've tried using variations of apply()
function paste()
being function want use, no avail. final result data frame of rows, looking - first 2 rows have been modified above startmonthv , endmonthv:
col1 col2 1 june 1, 2016 september 28, 2016 2 june 1, 2016 september 28, 2016 3 june 11 october 8 4 june 11, 2012 october 5 june 14, 2012 sept 27, 2012
i'm new r, , wondering if apply()
family do, or using function within plyr package. stackoverflow answer i've found either applies whole data frame or collapses data aggregate()
function.
thank you.
we can use mapply
, assign result @ corresponding rows:
days <- c("1,", "28,") monthdf[v0, ] <- mapply(paste, monthdf[v0, ], days, substr(sys.date(), 1, 4)) monthdf col1 col2 1 june 1, 2016 september 28, 2016 2 june 1, 2016 september 28, 2016 3 june 11 october 8 4 june 11, 2012 october 5 june 14, 2012 sept 27, 2012
here created vector days
according specific days append columns.
Comments
Post a Comment