0

Hello you wonderful people. I am new to R and matlab and been searching on the net to answer this but cant find anything.The problem is when I create a final matrix it just lists filenames.

The background behind this, so that you understand what I mean:

I am using R with inbuilt matlab and trying to read data from 15 .txt files.These 15 files all have 2 columns of data and 1686 rows. I am supposed to put these files into a vector of type list using a dir() function. I am then to access each element of this vector and pass it to read.table() function so that I can create a matrix N, which contains the data. As mentioned earlier, these files have 2 columns and I am told to extract the 1st column of each file and put it in the ith column of a matrix called X. X has 16 rows to represent the 15 files and a header row and 1687 columns for the data and dismissing the first column.

When I type X in the console bit of R studio it comes up with the Error in X[i, ] <- t(N[, 1]) : number of items to replace is not a multiple of replacement length.

I thought fair enough, I'll change X to a 15 by 15 matrix and it instead came up with a matrix containing the .txt file names, instead of the data I wanted. I first want to solve this error of file names before I try to solve the multiple replacement length error. Is their something wrong in my code? I have written the code below as if for 15 x 15 matrix to demonstrate the file name problem rather than 16 x 1687 as original:

# Clear workspace
rm(list=ls())
# Close any open graphics devices
graphics.off()
# Load additional packages
require(matlab, quietly=TRUE)
# Activate functions
source("auto.r")
source("mncn.r")
source("rangescale.r")
source("hcluster2.r")


filelist <- dir(path = "~/MSc/Course/Module3", pattern = "*.txt", all.files = FALSE,
    full.names = FALSE, recursive = FALSE,
    ignore.case = FALSE, include.dirs = FALSE, no.. = FALSE)

for (i in 1:length(filelist)) assign(filelist[i], read.table(filelist[i], sep="\t", header=T, row.names=1))

filelist <- as.matrix(filelist)
N <- filelist
X <- matrix (nrow = 15, ncol = 15)
X[i,] <- t(N[,1])
1
  • NO worries guys, fiiiiinaaaaaallly managed to do it took me hours of experimenting with a script a friend had sent me. They sent me this script.
    – Uniboy.r
    Nov 24, 2013 at 4:59

2 Answers 2

1

try out the apply functions, apply,sapply,mapply etc.

The one you want here since you are working with a list of files is lapply

Something similar to

datalist <- lapply(filelist, function(x) read.table(x, header = T))

So as an example: let me makes some text files

dat1 <- data.frame(x = rnorm(10),
                   y = rnorm(10))
dat2 <- data.frame(x = rnorm(5),
                   y = rnorm(5),
                   z = rnorm(5))

tmp <- c('dat1','dat2')
lapply(tmp, function(x) write.table(get(x), file = paste0(x,'.txt'), quote = F, row.names = F))

Now, here's what I would do in your situation.

filelist <- dir(path = '.', pattern = "*.txt", all.files = FALSE,
                full.names = FALSE, recursive = FALSE,
                ignore.case = FALSE, include.dirs = FALSE, no.. = FALSE)
> filelist
[1] "dat1.txt" "dat2.txt"

datalist <- lapply(filelist, function(x) read.table(x, header = T))

> datalist[1]
[[1]]
x          y
1   0.66407205  0.6380669
2  -0.85676390  0.3987090
3   1.23954448  0.7414505
4  -0.11823676 -0.1658559
5   1.41603036  0.5585108
6   2.16411026 -1.0918350
7  -0.54033389  0.3603718
8   0.01530916  0.5687294
9   0.06244237 -1.1000696
10 -0.15826680 -2.5999726

> datalist[2]
[[1]]
x           y           z
1  0.09159357 -0.80349348  0.04728642
2  0.21526376 -0.03353738  0.77563529
3  0.17405957 -0.68228875  0.72500158
4 -1.81829362 -1.59196023 -1.25275570
5 -1.92814438 -0.07139163 -0.08759747

And if you need to access individual data sets

> tmp <- datalist[[2]]
> dim(tmp)
[1] 5 3
> names(tmp)
[1] "x" "y" "z"
> tmp$y
[1] -0.80349348 -0.03353738 -0.68228875 -1.59196023 -0.07139163
0

OOOOH MYYY GOODNESS!!! toook me hours but managed to solve this by experimenting with a script that was given to me! I amendeded it to this:

sam <- dir(path = "~/MSc/Course/Module3", pattern = "*.txt", all.files = FALSE,
                full.names = FALSE, recursive = FALSE,  
                ignore.case = FALSE, include.dirs = FALSE, no.. = FALSE)

X <- matrix(nrow=1, ncol=1686)

for (i in 1:length(sam)) {N <- read.table(sam[i], sep="\t", header=F, row.names=NULL)
                          X <- rbind (X, N[,2])  }
rownames(X) <- c("temp", "A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3", "D1", "D2", "D3", "E1", "E2", "E3")
X <- X[-1,]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.