0

I am new in R.I have one question regarding my data set.

 S.NO   Type Measurements
 1  1        2.1
 2  2        3.3
 3  2        3.1
 4  3        2.7
 5  3        2.6
 6  3        4.5
 7  2        1.1
 8  3        2.2

suppose we have measurements in column 3 but their types are given in column 2.Each measurement is either type 1,type 2 or type 3.Now if we are interested to find only measurements corressponding to type 2(suppose),how we can do it in R? I am looking forward to response.

akrun
  • 874,273
  • 37
  • 540
  • 662
  • Can you provide an example of your data.frame using the [dput function](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? – Robert Krzyzanowski Nov 17 '14 at 15:37

2 Answers2

2

This is a basic subsetting question covered in most introductory R guides:

with(mydf, mydf[Type == 2, ])
#   S.NO Type Measurements
# 2    2    2          3.3
# 3    3    2          3.1
# 7    7    2          1.1
with(mydf, mydf[Type == 2, "Measurements"])
# [1] 3.3 3.1 1.1

You can also look at the subset function:

subset(mydf, subset = Type == 2, select = "Measurements")
#   Measurements
# 2          3.3
# 3          3.1
# 7          1.1
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
1
# make some data
testData$measurement=1:10
testData$Type=sample(1:3,10,replace=T)
testData=data.frame(testData)

# fetch only type 2
testData[testData$Type==2,]
# now only the measurements
testData[testData$Type==2,"measurement"]
phonixor
  • 1,623
  • 13
  • 18