查找与另一个矩阵中的相同位置对应的值

时间:2015-06-03 00:35:18

标签: r

我想为每个人获得因子分数。因素存储在数据框factors中我需要获取与data对应的值factors的另一个数据框中的值的平均值,并将其存储在{{{}}的新列中1}}。我为可怕的解释道歉。我希望我的榜样有所帮助,我很乐意回答问题!

data

我会尝试将其分解为步骤(据我理解的过程):

  • 在数据框factors<-data.frame(c(NA,2,NA),c(NA,3,1)) colnames(factors)<-c("v1","v2") row.names(factors)<-c("col1data","col2data","col3data") factors data<-data.frame(c(2,4,2),c(1,1,2),c(3,3,3)) colnames(data)<-c("col1data","col2data","col3data") row.names(data)<-c("person1","person2","person3") data #in dataframe factors row col2data is present (i.e. not NA) under factor V1 #go into dataframe data for each person and make a new column called v1 that holds the value of col2data #do this for factor v2 and average the values to come up with one number for each person. Final result data<-data.frame(c(2,4,2),c(1,1,2),c(3,3,3),c(1,1,2),c(2,3,2.5)) colnames(data)<-c("col1data","col2data","col3data","v1","v2(avg col2 and col3)") row.names(data)<-c("person1","person2","person3") data 的列中查找非NA的行名称
  • 将行名称与数据框factors列匹配。
  • data中对匹配的行名称进行求和并存储在名为data中列的列名称的新变量中(例如data),每个人

    < / p>

  • 2 个答案:

    答案 0 :(得分:0)

    您可以将data的行方式限制为相应的列:

    cbind(data, apply(factors, 2, function(x) rowMeans(data[,!is.na(x),drop=FALSE])))
    #         col1data col2data col3data v1  v2
    # person1        2        1        3  1 2.0
    # person2        4        1        3  1 2.0
    # person3        2        2        3  2 2.5
    

    答案 1 :(得分:0)

    我按照您的方式说明了您如何理解流程作为代码中的注释,以查看流程中每个步骤的执行位置。

    factors<-data.frame(c(NA,2,NA),c(NA,3,1))
    colnames(factors)<-c("v1","v2")
    row.names(factors)<-c("col1data","col2data","col3data")
    factors
    
    data<-data.frame(c(2,4,2),c(1,1,2),c(3,3,3))
    colnames(data)<-c("col1data","col2data","col3data")
    row.names(data)<-c("person1","person2","person3")
    data
    
    #find row names in a column of dataframe factors that are not NA
    not_na_rows_v1 <- rownames(factors)[!is.na(factors$v1)]
    not_na_rows_v2 <- rownames(factors)[!is.na(factors$v2)]
    not_na_rows_v1
    not_na_rows_v2
    #match row names to dataframe data columns.
    #Sum matching row names in data and store in new variable called the column name of the column in data (eg v1) for each person
    ###*note*### apply(...,1 ,mean) takes the mean for each row (the "1" means by row, "2" would mean by column)
    data[, 'v1'] <- data[, not_na_rows_v1]
    data[, 'v2'] <- apply(data[, not_na_rows_v2], 1, mean)
    data