1

How do I extract only random numbers(CD) for 'Trt' at time point 1.

ns <- 20
ans <- matrix(rep(0,200),nrow=100)
for(k in 1:100)
{ 
x1=rnorm(ns,0,1)
x2=rnorm(ns,5,5)
x3=rnorm(ns,10,5)
U=c(x1,x2,x3)
simdata=data.frame(CD=U,
                   Time=factor(rep(c(1,2,3),each=ns)),
                   treatment=sample(rep(c('Trt','placebo'),ns/2)))
ans[k,]=table(simdata$treatment)
}
simdata
Jaap
  • 81,064
  • 34
  • 182
  • 193
user3407190
  • 65
  • 1
  • 1
  • 8
  • `subset(simdata, Time == 1, select = "CD")`. – Gregor Thomas Apr 04 '14 at 18:07
  • May be I did not explain myself very well. I need to see the CD,Treatment displaying all the 10 'Trts' in time 1. Thank you – user3407190 Apr 04 '14 at 18:15
  • @user3407190 see my answer, it gives you exactly that – Jaap Apr 04 '14 at 18:18
  • @user3407190 I noticed that you *unaccepted* my answer. I like to know why. Was there something wrong? – Jaap Apr 11 '14 at 07:48
  • @Jaap sorry I thought you could accept as many correct answers as possible. In that case I will do the honorable thing. It is only fair that I accept yours instead since you were first to respond. – user3407190 Apr 11 '14 at 09:09
  • @user3407190 It's not possible to accept more than one answer. What you can do when you get several good answers, is giving the other answers an upvote (you need a minimum reputation of 15 before you can do that though). – Jaap Apr 11 '14 at 09:19

3 Answers3

1

You can do that in multiple ways:

simdata$CD[sim_data$Time == 1]

or use subset:

subset(simdata, Time == 1, select = "CD")

The former is recommended for use in scripts, the latter works well in interactive mode (R prompt).

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
0

You can subset for both conditions (treatment = "Trt" and Time = "1") like this:

smpl <- simdata[simdata$Time=="1" & simdata$treatment=="Trt",]

If you only want the CD column:

smpl <- simdata$CD[simdata$Time=="1" & simdata$treatment=="Trt",]
Jaap
  • 81,064
  • 34
  • 182
  • 193
  • A sidenote: it's better to use [`[` instead of `subset`](http://stackoverflow.com/questions/9860090/in-r-why-is-better-than-subset) – Jaap Apr 11 '14 at 09:22
0

I think you want CD for Timepoint "1" and Treatment ="Trt"

subset(simdata, Time == 1 & treatment == "Trt", select = "CD")

alternatively for the whole data frame

subset(simdata, Time == 1 & treatment == "Trt")
infominer
  • 1,981
  • 13
  • 17