Reputation: 343
x <- c("a", 2, 3, 1.0)
y <- c("b", 1, 6, 7.9)
z <- c("c", 1, 8, 2.0)
p <- c("d", 2, 9, 3.3)
df1 <- data.frame(x,y,z,p)
Here is a quick example data set, but it doesn't mirror exactly what im trying to do. Say I wanted to take 50 random samples from each level of factor in row 2 (in this case we only have 2 levels of the factor)... How would I go about coding that efficiently? I have a version working in a loop but it feels needlessly complex
edit: When I say I want to take 50 random samples I mean take 50 columns from each level of the factor.
Upvotes: 0
Views: 110
Reputation: 3648
You will need to extract a factor (assuming that 2nd row is a factor).
fact <- as.factor(as.matrix(df1[2,]))
And then work with the second column which you want to be a factor. For example, to sample all for the first value of factor
df1[,df1[2,]==levels(fact)[1],]
Or for getting exactly 50:
df1[,df1[2,]==levels(fact)[1],][1:50]
Upvotes: 1
Reputation: 173557
Maybe you're looking to do something like this:
x1 <- df1[,sample(c(1,4),50,replace = TRUE)]
x2 <- df1[,sample(c(2,3),50,replace = TRUE)]
...but your question is very confusing. "factor" refers to something very specific in R: a type of variable that is generally stored in a column of a data frame, never a row. Additionally, you appear to be forcing all your columns themselves to be factors (or characters possibly), which seems an odd way to store the value 3.3
.
Upvotes: 1