开发者

How do you read in a sparse incidence matrix in R or matlab?

i have a text file of unequal rows meaning each rows have different number of elements

something like

data1 7 6 6 5 6 7 8 9

data2 2 6 7

data3 93

each row is data collection of some kind. and i need to use each row as a collection of data

how do i read this into a dataframe or data matrix in R or matl开发者_如何学编程ab? thank you!


In R, I use this function for data that is ordered in rows of different length in a text file, presuming your example is a true representation of the text file you have. It returns a list, not a dataframe or a matrix. Unless the columns in your file are linked in some way, using a matrix or a dataframe doesn't make sense. A list gives you the right representation : a group of vectors, each representing a row, and named after the first element of the row.

readRows <- function(file,sep="\n",split=" ",...){
    tt <- strsplit(
              scan(file,what="list",sep=sep,...),
              split=split
    )
    out <- lapply(tt,function(i) as.numeric(i[-1]))
    names(out) <- sapply(tt,`[`,1)
    out
}

This returns a named list where the name of each element is the first item in the row, and the elements represent the lines of numbers. If your data is not numeric, you can adapt the function as needed.

zz <- textConnection("data1 12 33 12
data2 11
data3 33 44 25 51 42 11")
readRows(zz)
close(zz)


Save your data as a text file named dat.txt. Then, use:

dat <- read.table('dat.txt', fill=T)


Here is how you could read this data in MATLAB.

filename = 'input.txt';
fid = fopen(filename,'r');
rawdata = textscan(fid,'%s %[^\n]','HeaderLines',1);
fclose(fid);
numdata = cellfun(@str2num, rawdata{2},'uniformoutput',0);
names = rawdata{1};   

You can convert it to a structure, but make sure all names in the 1st column are unique or you will get an error.

try
    datastruct = cell2struct(numdata,names);
catch ME
    error('Cannot create data structure: %s', ME.message)
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜