0

I am getting this error

'Error: Argument 1 must have names'

can somebody help?

temp <- data.frame()
for (i in 2:3){
temp <- rbind(temp,without_first %>% filter(sz==i) %>% samp(i,replace=TRUE))
}

Data structure

id     timepoint  size
1001   time30      2
1001   time60      2
1001   time90      2
1002   time60      1
1002  time90       1

I am trying to randomly select 2 observations for id=1001,1 observations for id=1002 etc.I have many such subjects in dataset. My code which is given above is not working. Can someone suggest code in R ?

I have also tried

data<-%>% group_by(id)%>%sample_n(size,replace=TRUE)

does not work.Please suggest some code in R ?

A. Suliman
  • 12,923
  • 5
  • 24
  • 37

1 Answers1

1

No need to use for loop. You could directly specify size in sample_n using the first value from size column for each id.

library(dplyr)

df %>%
  group_by(id) %>%
  sample_n(first(size))


#    id timepoint  size
#  <int> <fct>     <int>
#1  1001 time60        2
#2  1001 time30        2
#3  1002 time90        1
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • @user11358720 You can consider to accept the answer by clicking on check mark next to vote button so that it can be marked as solved. – Ronak Shah Aug 20 '19 at 14:00