S jos
S jos

Reputation: 33

Subsetting a dataset, adding column and rows in R

I have a dataset that has 5 columns and 50 rows. I want to divide it into two parts, one with 35 rows and 15 in the other randomly. Then i would like to add another column to this dataset which contains value TRUE/FALSE. TRUE if the row belongs to the 35 randomly selected rows and FALSE if it belongs to the 15. How do i achieve it in R... All help is greatly appreciated.. Thanks

Upvotes: 0

Views: 598

Answers (1)

akrun
akrun

Reputation: 887421

We create a vector of 'TRUE/FALSE' elements using rep by specifying the times to replicate the 'TRUE/FALSE' values, sample it, and create a new column ('ind') by assigning the output. Then, split the dataset into a list of 2 data.frames by 'ind' column.

df1$ind <- sample(rep(c(TRUE, FALSE), times = c(35, 15)))
split(df1, df1$ind)

data

set.seed(24)
df1 <- as.data.frame(matrix(sample(9, 50*5, replace=TRUE), ncol=5))

Upvotes: 2

Related Questions