对R中数据帧中的每个变量进行矢量化或循环重复操作

时间:2019-05-30 19:47:52

标签: r loops vectorization apply

我编写了一个脚本,用于对数据框中的单个变量执行计算。现在,我需要对总共1007个变量重复此操作。对于每个变量,计算得出2个值(出现频率和丰度)。目标是对每个变量将两个值相互绘制的图。对于如何编写循环或如何使用apply函数对数据帧中的每个变量执行操作的建议,我将不胜感激。 (第二步是使用这些值创建一个矩阵,以便我可以绘制它们。)谢谢!

library(dplyr)
data <- read.csv("cap_otu.csv", header = T)
prey <- filter(data, A100 > 0)
dim(prey)
prey2 <- subset(prey, select = -c(Site, Individual))
dim(prey2)
###
x <- as.matrix(prey2)
#get the sum for all rows (total abundance for each stomach)
x2 <- apply(x, 1, sum)
#get the sum for variable of interest e.g. A100 
x3 <- colSums(x)
x4 <- as.data.frame(x3)
x5 <- x4['A100',] # get total abundance value for A100
x5
x6 <- sum(x2) #sum the total abundance values of all individuals     
#that contain A100 
#calculate percentage value for abundance variable A100 against abundance  
# of all individuals containing variable A100
Var_A100 <- x5/x6*100  
Var_A100 # this is the abundance value for variable A100
# 9.835596 %
# calculate Frequency of Occurrence (FO) 
FO_A100 <- 15/153 
#15 = nr of individuals containing variable A100, 153 = total nr. of 
#individuals
FO_A100
# 0.09803922

0 个答案:

没有答案