DevanDevak DevanDevak - 1 year ago 92
R Question

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")

The output of dataframe af is :

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download