Search code examples
rfilevectorrow

Convert a tab delimited file into vector in r and use it to process


I want to convert each row of tab delimited file (.txt extension into a vector) and use that vector for furthur processing and then store the results in another file . So far I have

 df <- read.csv("matrix_pvalues.txt")
head(df)
X1.00E.08.2.75E.45.7.15E.08
 1 1.00E-18\t1.00E+00\t3.42E-05
 2 1.00E-23\t8.86E-42\t0.017703944
 3 1.00E-16\t1.75E-70\t0.0121
 4 1.00E-30\t1.20E-07\t1
 5 1.00E-18\t1.00E+00\t0.004553174
 6 1.00E-23\t1.00E+00\t0.049965122
> df_vector<-as.vector(t(df))
> head(df_vector)
 [1] "1.00E-18\t1.00E+00\t3.42E-05"    "1.00E-23\t8.86E-42\t0.017703944"
 [3] "1.00E-16\t1.75E-70\t0.0121"      "1.00E-30\t1.20E-07\t1"          
 [5] "1.00E-18\t1.00E+00\t0.004553174" "1.00E-23\t1.00E+00\t0.049965122"

Any input will be helpful


Solution

  • Assuming that you have the same number of values in each line:

    DF <- read.delim(text="1.00E-08\t2.75E-45\t7.15E-08
    1.00E-18\t1.00E+00\t3.42E-05
    1.00E-23\t8.86E-42\t0.017703944
    1.00E-16\t1.75E-70\t0.0121
    1.00E-30\t1.20E-07\t1
    1.00E-18\t1.00E+00\t0.004553174
    1.00E-23\t1.00E+00\t0.049965122", header=FALSE)
    

    Notice I had to guess at the first row, but your read.csv assumes the first row contains column names so it tried to convert that data. Now each row can be extracted as a vector, e.g.:

    DF[1, ]
    #      V1       V2       V3
    # 1 1e-08 2.75e-45 7.15e-08