3

I am trying to sample 2 columns of a dataframe but the sample function is allowing me only one column to sample not both columns(Campaignid,CampaignName) at once.

Is there a way to sample like I wanted!

camp.d <- data.frame(Campaignid=c(121,132,133,143,153),
                     CampaignName=c('a','b','c','d','e'))

#allows only one column
a <- sample(camp.d$Campaignid, 100, replace = TRUE)

Expected:

Campaignid CampaignName
  121             a
  121             a
  133             c
  132             b
  132             b
...
prog
  • 1,073
  • 5
  • 17

3 Answers3

3

I think you need this -

sampled_data <- camp.d[sample(nrow(camp.d), 100, replace = T), ]

head(sampled_data)

    Campaignid CampaignName
2          132            b
5          153            e
3          133            c
3.1        133            c
2.1        132            b
4          143            d
Shree
  • 10,835
  • 1
  • 14
  • 36
2

You could use the sample call the slice the full dataframe


camp.d[sample(camp.d$Campaignid, 100), ]

Jkennedy559
  • 134
  • 1
  • 9
1

You can try:

as.data.frame(lapply(camp.d, sample, size = 100, replace = TRUE))

    Campaignid CampaignName
1          132            a
2          133            c
3          143            a
4          132            e
5          133            c
6          143            a
7          132            c
8          153            a
9          121            c
10         132            b
tmfmnk
  • 38,881
  • 4
  • 47
  • 67