从R中的数据框中提取行和列

Extracting row and column from data frame in R

我有一个 csv 文件,我想从中提取第二列值到数据框中,示例 csv 文件如下图所示

以下是我写的脚本

ICVdir <- "/media/dev/Daten/Task1/T1_Images"
#loding csv file from ICV
mycsv  <- list.files(ICVdir,pattern = "*.csv",full.names = T )
af<- read.csv(file = mycsv,header = TRUE, sep = "\t")
ICV<- as.data.frame(af[,2],drop=FALSE)

data.frameaf的输出是:

 subj_id.eTIV_FLIRT.FASTvol_noCSF
1             Sub1,0.824198,1360784
2             Sub2,0.792987,1350024
3             Sub3,0.831011,1304154
4             Sub4,0.840316,1277706
5             Sub5,0.928503,1562892
6             Sub6,0.840962,1367487
7             Sub7,0.776565,1486331
8             Sub8,0.845449,1394665
9             Sub9,0.924351,1496015
10           Sub10,0.885719,1450941
11       Mean eTIV_FLIRT,0.8490061,

我想提取第 eTIV_FLIRT 列中的值(这是数据框中的第二列

我正在获得以下输出

 Error in `[.data.frame`(af, , 2) : undefined columns selected

请告诉我我的代码有什么问题

在你的代码中

af<- read.csv(file = mycsv,header = TRUE, sep = "\t")

您指定制表符作为分隔符。 您的 data.frame 仅包含 一个 列。例如,第一行是单个值 Sub1,0.824198,1360784.

由于只有一列,因此无法使用 af[,2] 提取第二列。

只需删除 sep(保留默认逗号)即可解决您的问题。