DevanDevak
DevanDevak

Reputation: 41

Extracting row and column from data frame in R

I have a csv file from which i would like to extract second column values into a data frame the example csv file looks like the image below

enter image description here

Following is the script that i written

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)

The output of data.frame af is :

 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,

I would like to extract the values in column eTIV_FLIRT( which is second column into a data frame

I am obtaining following output

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

kindly please let me know what is wrong in my code

Upvotes: 0

Views: 392

Answers (1)

Martin Nyolt
Martin Nyolt

Reputation: 4660

In your code

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

you specify a tab as the separator. Your data.frame contains only one column. E.g., the first row is the single value Sub1,0.824198,1360784.

As you have only one column, you cannot extract the second with af[,2].

Simply removing the sep (leaving the default comma) should solve your problem.

Upvotes: 2

Related Questions