Adhil
Adhil

Reputation: 1858

How to get Values from a particular column in R

I am reading in a text file and saving that data in a table. I want to pick values from column "Vf2" Where the value in the column "Site" is equal to 2. how do I do so in R?

Vf2 Site
2.76    1
2.32    2
2.56    3
2.45    2
2.76    1
2.98    3
2.58    1
2.42    2

this is what I have so far.

afile <- read.table("C:/blufiles/WFRHN205_700.blu", skip = 2, header = TRUE, sep="\t")
afile["Vf2"]

Upvotes: 1

Views: 754

Answers (1)

Stewart Macdonald
Stewart Macdonald

Reputation: 2132

Something like this should work:

# Set up the dataframe
Vf2 <- c(2.76, 2.32, 2.56, 2.45, 2.76, 2.98, 2.58, 2.42)
Site <- c(1, 2, 3, 2, 1, 3, 1, 2)
afile <- data.frame(Vf2, Site)

# Select all values in the Vf2 column where the corresponding Site value is 2
afile$Vf2[afile$Site == '2']

# if you want to select the entire row:
afile[afile$Site == '2', ]

Upvotes: 1

Related Questions