iterating over columns and sample

Viewed 51

I'm new to R.
I have a data frame that each column contains 1 and 0 only.
I need to sample 10,000 times each column in the data frame, and then create new row that contains the probability of choosing 1 (I guess it's mean), and then binding it to the date frame.
I know how to create another row and binding it to the the original data frame.
But how to itearate over columns and sample (including doing mean) seperatly and then modify the new row accordingly? The syntax is little bit tricky for me.

2 Answers

Since all of your columns need to be sampled, you do not need to iterate. You can randomly sample n number of rows from a data frame in two different ways:

  1. Using Base R
  2. Using dplyr package
# Using Base R with built in mtcars data

> library(datasets)  # comes by default in R

> cars_df <- mtcars

> set.seed(123) # allows us to reproduce the data

> cars_df_15 <- cars_df[sample(nrow(cars_df), 15),]

> cars_df_15
                    mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Maserati Bora      15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
Cadillac Fleetwood 10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
Honda Civic        30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
Merc 450SLC        15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
Datsun 710         22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
Merc 280           19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
Fiat 128           32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
Dodge Challenger   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    4
Hornet Sportabout  18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
Toyota Corolla     33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
Ford Pantera L     15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
AMC Javelin        15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
Ferrari Dino       19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
Merc 230           22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2

# Using dplyr package

> library(dplyr)

> cars_df <- mtcars

> set.seed(123)

> cars_df_15 <- sample_n(cars_df, 15)

> cars_df_15
                    mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Maserati Bora      15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
Cadillac Fleetwood 10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
Honda Civic        30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
Merc 450SLC        15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
Datsun 710         22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
Merc 280           19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
Fiat 128           32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
Dodge Challenger   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    4
Hornet Sportabout  18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
Toyota Corolla     33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
Ford Pantera L     15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
AMC Javelin        15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
Ferrari Dino       19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
Merc 230           22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
# 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)

If you have only 0/1 in your columns then you can simply calculate the column means with colMeans. Then simply do a sim on all columns using rbinom and rbind to your orgininal data frame. Like this:

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)
Related