Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.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_Csv_Statistics_Summary - Fatal编程技术网

R.统计摘要

R.统计摘要,r,csv,statistics,summary,R,Csv,Statistics,Summary,如何同时为来自不同物种(第1列中)的多个类别(第1行中的不同测量值)生成一些汇总统计数据(平均值、标准差、范围、样本大小),并使用“write.csv()打印到一个数据文件。如果我一次只处理一个物种,我可以做得很简单,但我想将所有物种的所有数据放在一个.csv文件中,一次生成所有统计数据总和。”我知道你在说什么。假设您想要得到平均值、标准偏差、范围和样本量。因为R给出的函数范围不是一个数字,而是数据集中最小的数字和最大的数字,这给了我一个问题。神奇之处在于塔普利()。我刚刚使用了transpos

如何同时为来自不同物种(第1列中)的多个类别(第1行中的不同测量值)生成一些汇总统计数据(平均值、标准差、范围、样本大小),并使用“write.csv()打印到一个数据文件。如果我一次只处理一个物种,我可以做得很简单,但我想将所有物种的所有数据放在一个.csv文件中,一次生成所有统计数据总和。”

我知道你在说什么。假设您想要得到平均值、标准偏差、范围和样本量。因为R给出的函数范围不是一个数字,而是数据集中最小的数字和最大的数字,这给了我一个问题。神奇之处在于塔普利()。我刚刚使用了transpose t()和as.matrix,使其更容易放入数据帧

无论如何,看看内置的iris数据集

data(iris)
我将给出所有这些的平均值、sd和样本大小(仅与萼片长度有关),用rbind将所有值写入数据帧的行中,最后用rownames()给出行名

只要这样做:

mean_sepal_length = t(as.matrix(tapply(iris$Sepal.Length, iris$Species, mean)))
mean_sepal_length

sd_sepal_length = t(as.matrix(tapply(iris$Sepal.Length, iris$Species, FUN = sd)))
sd_sepal_length


sample_size_sepal_length = t(as.matrix(tapply(iris$Sepal.Length, iris$Species, FUN = length)))
sample_size_sepal_length


df_sepal_length <- data.frame(mean_sepal_length)
df_sepal_length

View(df_sepal_length)

df_sepal_length = rbind(df_sepal_length, sd_sepal_length)

df_sepal_length = rbind(df_sepal_length, sample_size_sepal_length)

rownames(df_sepal_length) <- c("Mean_sepal_length", "sd_sepal_length", "size_sepal_length")

write.csv(df_sepal_length, "C:/Users/me/Documents/tapply_miracle.csv")
mean_sepal_length=t(作为矩阵(tapply(iris$sepal.length,iris$Species,mean)))
平均萼片长度
sd_萼片长度=t(如基质(tapply(鸢尾$sepa.length,鸢尾$Species,FUN=sd)))
萼片长度
样本大小萼片长度=t(如基质(tapply(鸢尾$sepal.length,鸢尾$Species,FUN=length)))
样本大小萼片长度

df_sepal_length我在考虑我当天给出的答案,当我意识到tapply函数可以接受索引变量作为列表时,我认为这可能会更好。在我的例子中,我只知道tapply可以对一个因素进行分类,但我们可以指定多个因素。诀窍是使用函数melt()将iris数据帧从宽到长的形式进行融合,使其更具可读性,然后使用列表参数进行tapply:

       > install.packages("reshape2")
        > library(reshape2)

    # I used melt to restyle the iris dataframe from wide to long turning the many columns into rows with less columns, and I coerced the iris dataset back to a dataframe.   

        > iris_melt <- data.frame(melt(data = iris, id = "Species", variable.name = "iris_factors", value.name = "iris_dimensions_cm"))


   > head(iris_melt)
  Species iris_factors iris_dimensions_cm
1  setosa Sepal.Length                5.1
2  setosa Sepal.Length                4.9
3  setosa Sepal.Length                4.7
4  setosa Sepal.Length                4.6
5  setosa Sepal.Length                5.0
6  setosa Sepal.Length                5.4
> tapply(X = iris_melt$iris_dimensions_cm, INDEX = list(iris_melt$iris_factors, iris_melt$Species), FUN = sd)
                setosa versicolor virginica
Sepal.Length 0.3524897  0.5161711 0.6358796
Sepal.Width  0.3790644  0.3137983 0.3224966
Petal.Length 0.1736640  0.4699110 0.5518947
Petal.Width  0.1053856  0.1977527 0.2746501
如果我们改变索引列表中因子的顺序,我们可以通过翻转行和列来获得以稍微不同的格式呈现给我们的相同信息:

> tapply(X = iris_melt$iris_dimensions_cm, INDEX = list(iris_melt$iris_factors, iris_melt$Species), FUN = mean)
             setosa versicolor virginica
Sepal.Length  5.006      5.936     6.588
Sepal.Width   3.428      2.770     2.974
Petal.Length  1.462      4.260     5.552
Petal.Width   0.246      1.326     2.026  
获得标准偏差很容易。只要改变有趣的论点:

       > install.packages("reshape2")
        > library(reshape2)

    # I used melt to restyle the iris dataframe from wide to long turning the many columns into rows with less columns, and I coerced the iris dataset back to a dataframe.   

        > iris_melt <- data.frame(melt(data = iris, id = "Species", variable.name = "iris_factors", value.name = "iris_dimensions_cm"))


   > head(iris_melt)
  Species iris_factors iris_dimensions_cm
1  setosa Sepal.Length                5.1
2  setosa Sepal.Length                4.9
3  setosa Sepal.Length                4.7
4  setosa Sepal.Length                4.6
5  setosa Sepal.Length                5.0
6  setosa Sepal.Length                5.4
> tapply(X = iris_melt$iris_dimensions_cm, INDEX = list(iris_melt$iris_factors, iris_melt$Species), FUN = sd)
                setosa versicolor virginica
Sepal.Length 0.3524897  0.5161711 0.6358796
Sepal.Width  0.3790644  0.3137983 0.3224966
Petal.Length 0.1736640  0.4699110 0.5518947
Petal.Width  0.1053856  0.1977527 0.2746501

现在我基本上不必使用Rbind

欢迎来到StackOverflow!请快速阅读并签出。然后,您可以回来编辑您的问题,添加一个示例和一些代码来显示您尝试了什么,以及有助于澄清您的问题的任何其他内容。非常感谢。我可以分别获得每个物种的所有这些数据,但如果我想在同一个数据矩阵(.csv文件)中获得多个物种的数据,我希望一次完成所有这些数据,而不是将矩阵分割成单独运行的特定于单个物种的数据矩阵。有剧本吗?