2017-04-14 70 views
1

我正在閱讀文本文件並將該數據保存在表中。我想從列「Vf2」中選擇值,其中「Site」列中的值等於2.如何在R中執行此操作?如何從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 

這是我到目前爲止。

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

回答

1

像這樣的東西應該工作:

# 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', ]