Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何使用向量循环数据帧中的一组特定列名称?_R_Loops_Vector - Fatal编程技术网

R 如何使用向量循环数据帧中的一组特定列名称?

R 如何使用向量循环数据帧中的一组特定列名称?,r,loops,vector,R,Loops,Vector,我已经为这个问题挣扎了好几天,我对R还是个新手,所以我放弃了,我希望你们中的任何人都能帮助我 我想计算一个特定变量的汇总统计数据,该变量由我想循环的不同变量分组。我不想每次都复制粘贴语法并更改分组变量。我使用了for循环和lappy以及vector(存储不同分组变量的地方) 我认为问题在于我的数据帧找不到我存储在向量中的列名 我的代码如下所示: snp_EPA <- c('rs3798713_C', 'rs174550_C', 'rs174574_A', 'rs174448_C') #Ve

我已经为这个问题挣扎了好几天,我对R还是个新手,所以我放弃了,我希望你们中的任何人都能帮助我

我想计算一个特定变量的汇总统计数据,该变量由我想循环的不同变量分组。我不想每次都复制粘贴语法并更改分组变量。我使用了
for
循环和
lappy
以及vector(存储不同分组变量的地方)

我认为问题在于我的数据帧找不到我存储在向量中的列名

我的代码如下所示:

snp_EPA <- c('rs3798713_C', 'rs174550_C', 'rs174574_A', 'rs174448_C') #Vector of grouping variables

for (i in snp_EPA) {
FA %>% group_by(as.name(i)) %>% summarise(FA, bce_c20_5n_3)
} #For loop I tried, didn't work

epa <- lapply(snp_EPA, function(x) {describeBy(FA$bce_c20_5n_3, as.name(x))})
lapply(epa, print) #lapply function I used, still didn't work....
snp_EPA%分组依据(如名称(i))%%>%总结(FA,bce_c20_5n_3)
}#对于循环,我尝试过,但没有成功

epa我们确实需要关于您的数据的更多信息和使用
dput(data)
的小样本。我可以告诉你一些方法来得到你想要的东西,这可能会让你开始。我将使用R随附的
iris
数据集:

data(iris)
str(iris)
# 'data.frame': 150 obs. of  5 variables:
#  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
#  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
#  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
#  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
# $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
该数据集包括对三种不同虹膜的4次测量。获取描述性统计数据的一种简单方法是使用
split
summary

iris.split <- split(iris, iris$Species)
lapply(iris.split, summary)
# $setosa
#  Sepal.Length    Sepal.Width     Petal.Length    Petal.Width          Species  
#  Min.   :4.300   Min.   :2.300   Min.   :1.000   Min.   :0.100   setosa    :50  
#  1st Qu.:4.800   1st Qu.:3.200   1st Qu.:1.400   1st Qu.:0.200   versicolor: 0  
#  Median :5.000   Median :3.400   Median :1.500   Median :0.200   virginica : 0  
#  Mean   :5.006   Mean   :3.428   Mean   :1.462   Mean   :0.246                  
#  3rd Qu.:5.200   3rd Qu.:3.675   3rd Qu.:1.575   3rd Qu.:0.300                  
#  Max.   :5.800   Max.   :4.400   Max.   :1.900   Max.   :0.600           
# . . . results for other 3 measurements

这些示例使用了所有数字列,但您只能选择带有
iris[,1:3]
的一些列来获得前三列,或者
iris[,c(1,4)]
来获得第一列和第四列。

您希望计算什么类型的汇总?只需要基本的汇总统计信息,如平均值、标准偏差等。
library(RcmdrMisc)   # You will have to install it the first time with `install.packages("RcmdrMisc)`.
numSummary(iris[, -5], groups=iris$Species)
# 
# Variable: Sepal.Length 
#             mean        sd   IQR  0%   25% 50% 75% 100%  n
# setosa     5.006 0.3524897 0.400 4.3 4.800 5.0 5.2  5.8 50
# versicolor 5.936 0.5161711 0.700 4.9 5.600 5.9 6.3  7.0 50
# virginica  6.588 0.6358796 0.675 4.9 6.225 6.5 6.9  7.9 50 
# . . . results for three other measurements.