使用“apply”和行索引作为应用函数的参数在数据帧行上应用函数的方法

时间:2015-08-19 09:33:33

标签: r apply

以下是与我的问题相关的工作案例:

# build a sample data frame
r1 <- c("XX","FV","U1","U2","U1")
r2 <- c("U1","U2","U1","U2","U2")
r3 <- c("XX","U2","XX","U2","U2")
test.df <- as.data.frame(rbind(r1,r2,r3))
# build the desired function. This function will return a result based on the evaluation of a given condition
# what the function does is not that important, just of note is that requires two arguments that will be sent from 'apply' function (edited comment)
fun.Majority <- function(my.row1, initial.value) {
  ifelse ((sum(grepl("U1", my.row1))==2) & (sum(grepl("U2", my.row1))==2), result <- "U1", result <- initial.value)
  return(result)
}
# create a new data frame with the first column containing the indices of the rows
test.df.idx <- cbind(1:nrow(test.df), test.df)
test.df.idx
#    1:nrow(test.df) V1 V2 V3 V4 V5
# r1               1 XX FV U1 U2 U1
# r2               2 U1 U2 U1 U2 U2
# r3               3 XX U2 XX U2 U2

# use apply on test.df.idx calling the fun.Majority. 
test.df$Mj <- apply(test.df.idx, MARGIN=1, function(my.row) fun.Majority(my.row[c(-1,-5)], as.character(test.df.idx[as.numeric(my.row[1]),6])))
# This is the relevant line to my issue. 
# Is there another way to pass the row index to my function without calling it from the test.df.idx as my.row[1]? 
# Is 'apply' allowing such operation?

# here is the final output
test.df
#    V1 V2 V3 V4 V5 Mj
# r1 XX FV U1 U2 U1 U1
# r2 U1 U2 U1 U2 U2 U1
# r3 XX U2 XX U2 U2 U2

对于fun.Majority()的第二个参数,我需要告诉R使用在每行的第一个位置找到的行索引,即my.row [1]

这个程序还有其他替代方法吗?我的意思是不必创建一个新的数据框,其中一列应包含每行的索引。 也许在调用函数(my.row)里面应用时,可以有另一种方法传递给函数fun.Majority()行的索引。 (请注意,正如我在标题中所提到的,我希望保留在“应用”功能的背景下,因为我自己的原因)

谢谢, 的Valentin

0 个答案:

没有答案