I am interested in creating a function using apply/ sapplyor Mapthat will iterate over the available columns in and replace the values ββin each column with the consistent values ββfrom the data frame, available in the unnamed list of data frames, with the list item index corresponding to the column number of the data frame . dta dta
Example
Defined objects:
set.seed(1)
size <- 20
dta <-
data.frame(
unitA = sample(LETTERS[1:4], size = size, replace = TRUE),
unitB = sample(letters[16:20], size = size, replace = TRUE),
unitC = sample(month.abb[1:4], size = size, replace = TRUE),
someValue = sample(1:1e6, size = size, replace = TRUE)
)
lstMeta <- list(
data.frame(
V1 = c("A", "B", "D"),
V2 = c("Letter A", "Letter B", "Letter D")
),
data.frame(
V1 = c("t", "q"),
V2 = c("small t", "small q")
),
data.frame(
V1 = c("Mar", "Jan"),
V2 = c("March", "January")
)
)
Desired Results
When applied to, the function should return the corresponding extraction below: dtadata.frame
unitA unitB unitC someValue
Letter B small t Apr 912876
Letter B small q March 293604
C s Apr 459066
Letter D p March 332395
Letter A small q March 650871
Letter D small q Apr 258017
Letter D p January 478546
C small q Feb 766311
C small t March 84247
Letter A small q March 875322
Letter A r Feb 339073
Letter A r Ap 839441
C r Feb 346684
Letter B p January 333775
Letter D small t January 476352
(...)
Existing approach
replaceLbls <- function(dataSet, lstDict) {
sapply(seq_along(dataSet), function(i) {
dtaDict <- lstDict[[i]]
dataSet[,i][match(dataSet[,i], dtaDict[,1])] <- dtaDict[,2][match(dtaDict[,1], dataSet[,i])]
})
}
replaceLbls(dataSet = dta, lstDict = lstMeta)
Of course, the approach proposed above does not work, as it will try to use NAin appointments; but he summarizes what I want to achieve:
x[...] <- m: NAs : : [<-.factor(*tmp*, match(dataSet[,
i], dtaDict[, 1]), value = c(NA,: , NA
: