i'm sure there easy solution this, i'm relatively new using r, thank patience!
i have 2 datasets. first (data) has 44 columns , x rows long. second (datb) has 4 columns , 22 rows. each row in datb corresponds 2 columns in data. made function transform data based on information. however, have type function 22 times - each of 22 rows(see below). 22 rows not bad, datb quite large , awful have type manually.
a <- c(13,14,15,16) data <- matrix(sample(a, 100, replace=true), ncol=2) datb <- matrix(a, ncol=4) change <- function(datx, bx, wx, verx,set) { ifelse (datx[,bx:wx] == verx[set,1],1, ifelse(datx[,bx:wx] == verx[set,2],2, ifelse(datx[,bx:wx]==verx[set,3],3,4))) } set1 <- change(data,1,2,datb,1) set2 <- change(data,3,4,datb,2) set3 <- change(data,5,6,datb,3) set4 <- change(data,7,8,datb,4) ..... set22<- change(data,43,44,datb,22) is there way can use sapply this? if 1 column in 1 dataset, no problem. use: sapply(colnames(x), function(y) somefunction(x[,y])). it's tricky me though because i'm trying loop across 2 columns in data , 1 row in datb @ same time.
so more clear, don't need function, in applying 2 datasets. although if different function work better this, open it.
****edited include sample data set1.
Comments
Post a Comment