R 迭代列和示例
我是R.的新手。R 迭代列和示例,r,R,我是R.的新手。 我有一个数据框,每列只包含1和0。 我需要对数据框中的每列进行10000次采样,然后创建包含选择1概率的新行(我猜这是平均值),然后将其绑定到日期框。 我知道如何创建另一行并将其绑定到原始数据帧。 但是如何分别对列和样本(包括doing Means)进行分析,然后相应地修改新行?语法对我来说有点棘手。因为所有列都需要采样,所以不需要迭代。您可以通过两种不同的方式从数据帧中随机抽样n行数: 使用基数R 使用dplyr软件包 #使用带内置mtcars数据的Base R >库(数据集
我有一个数据框,每列只包含
1
和0
。我需要对数据框中的每列进行10000次采样,然后创建包含选择1概率的新行(我猜这是平均值),然后将其绑定到日期框。
我知道如何创建另一行并将其绑定到原始数据帧。
但是如何分别对列和样本(包括doing Means)进行分析,然后相应地修改新行?语法对我来说有点棘手。因为所有列都需要采样,所以不需要迭代。您可以通过两种不同的方式从数据帧中随机抽样n行数:
#使用带内置mtcars数据的Base R
>库(数据集)#默认在R中出现
>cars_df set.seed(123)#允许我们复制数据
>车?车?车?车?车?车?车?车
mpg气缸显示hp drat wt qsec与am齿轮carb
玛莎拉蒂波拉15.08301.03353.543.57014.600158
凯迪拉克Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 3 4
本田思域30.4 4 75.7 52 4.93 1.615 18.52 1 4 2
Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 3
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 4 1
Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
菲亚特128 32.4 78.7 66 4.08 2.200 19.47 1 4 1
道奇挑战者15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4
大黄蜂运动约18.7 8 360.0 175 3.15 3.440 17.02 0 3 2
丰田花冠33.9 4 71.1 65 4.22 1.835 19.90 1 4 1
福特Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 01 5 4
AMC标枪15.2 8 304.0 150 3.15 3.435 17.30 0 3 2
法拉利迪诺19.7 6 145.0 175 3.62 2.770 15.50 01 5 6
Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
#使用dplyr包
>图书馆(dplyr)
>种子车(123)
>车?车?车?车?车?车?车?车
mpg气缸显示hp drat wt qsec与am齿轮carb
玛莎拉蒂波拉15.08301.03353.543.57014.600158
凯迪拉克Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 3 4
本田思域30.4 4 75.7 52 4.93 1.615 18.52 1 4 2
Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 3
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 4 1
Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
菲亚特128 32.4 78.7 66 4.08 2.200 19.47 1 4 1
道奇挑战者15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4
大黄蜂运动约18.7 8 360.0 175 3.15 3.440 17.02 0 3 2
丰田花冠33.9 4 71.1 65 4.22 1.835 19.90 1 4 1
福特Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 01 5 4
AMC标枪15.2 8 304.0 150 3.15 3.435 17.30 0 3 2
法拉利迪诺19.7 6 145.0 175 3.62 2.770 15.50 01 5 6
Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
#然后可以使用sapply()和colmeans()函数来获取平均值
#因为所有列都有相同的数字数据。在
#我在上面创建的汽车将使用
#获取多个列的平均值
如果列中只有0/1,则只需使用colMeans
计算列平均值即可。然后,只需对原始数据帧使用rbinom
和rbind
对所有列执行sim即可。像这样:
x <- sample(0:1, 1000, replace = TRUE)
m <- matrix(x, nrow = 100)
df <- as.data.frame(m) # Example data frame
means <- colMeans(df)
simmeans <- sapply(means, function(x) mean(rbinom(10000, 1, x)))
df2 <- rbind(df, simmeans)
x我需要单独的每一列……每一列都有不同的规则,添加的行列显示了每一列的概率规则有什么不同?不管规则如何,您仍然需要从每列中抽取10000个样本吗?我还添加了其他代码,用于创建列平均值并将其作为行附加到新数据集。@Shay you在文章中提到,您希望添加一个新行,显示每列1和0的平均值(在本例中为概率)。你能解释一下“添加行列”是什么意思吗?
# Then you can use sapply() and colmeans() functions to get the means
# since all of your columns have the same numeric data. In the
# cars_df_15 I created above I would use
# Get Mean of the multiple columns
row_with_means <- colMeans(cars_df_15[sapply(cars_df_15, is.numeric())])
# is.numeric checks that a given column has numeric data
# do not confuse it with is.Numeric
# is.Numeric differs from is.numeric in that data.frames with all columns numeric are accepted as numeric.
# now use row bind function rbind() to add this new row to the dataset
# with 10,000 rows (in my example 15)
cars_df_15_with_means <- rbind(cars_df_15, row_with_means)
x <- sample(0:1, 1000, replace = TRUE)
m <- matrix(x, nrow = 100)
df <- as.data.frame(m) # Example data frame
means <- colMeans(df)
simmeans <- sapply(means, function(x) mean(rbinom(10000, 1, x)))
df2 <- rbind(df, simmeans)