读取水平组织的CSV文件 [英] Reading a CSV file organized horizontally
问题描述
在R中,是否有一个函数 read.csv
读取文件的头部在左侧(或右侧),而不是顶部和数据从左到右组织?
所以数据看起来像:
var1,1,2,3,4,5
查看 read.table
和 read.csv
,没有什么似乎弹出。我看到使用这些函数的最好的选择是使用 read.table
然后构造另一个表,其中的列是原始数据的行等等。
假设您的文件名为data.csv,它包含:
var1,1,2,3,4,5,6
var2,2.1,3.9,4.6,5.2,6.1
var3,M, F,M,F,M,M
注意 var1
和 var3
有6个值,但 var2
只有5.
所以,读取数据,转置它,然后使用 read.csv
。
read.tcsv = function(file,header = TRUE,sep =,,...){
n = max(count.fields(file,sep = sep),na.rm = TRUE )
x = readLines(file)
.splitvar = function(x,sep,n){
var = unlist(strsplit(x,split = sep))
length(var)= n
return(var)
}
x = do.call(cbind,lapply(x,.splitvar,sep = sep,n = n) )
x = apply(x,1,paste,collapse = sep)
out = read.csv(text = x,sep = sep,header = header,...)
return然后,您可以执行以下操作:
read.tcsv(data.csv)
var1 var2 var3
1 1 2.1 M
2 2 3.9 F
3 3 4.6 M
4 4 5.2 F
5 5 6.1 M
6 6 NA M
In R, is there a function like read.csv
that reads in files where the headers are on the left (or right) as opposed to the top and the data is organized from left to right?
So the data would look like:
var1,1,2,3,4,5
Looking at the documentation for read.table
and read.csv
, nothing seems to pop out. The best option I see using those functions is to use read.table
and then construct another table whose columns are the rows of the original data and so forth.
解决方案 Let's say your file is called 'data.csv' and it contains:
var1,1,2,3,4,5,6
var2,2.1,3.9,4.6,5.2,6.1
var3,M,F,M,F,M,M
Note var1
and var3
have 6 values but var2
has only 5.
So, the idea is to read the data, transpose it and then use read.csv
.
read.tcsv = function(file, header=TRUE, sep=",", ...) {
n = max(count.fields(file, sep=sep), na.rm=TRUE)
x = readLines(file)
.splitvar = function(x, sep, n) {
var = unlist(strsplit(x, split=sep))
length(var) = n
return(var)
}
x = do.call(cbind, lapply(x, .splitvar, sep=sep, n=n))
x = apply(x, 1, paste, collapse=sep)
out = read.csv(text=x, sep=sep, header=header, ...)
return(out)
}
Then, you can do:
read.tcsv("data.csv")
var1 var2 var3
1 1 2.1 M
2 2 3.9 F
3 3 4.6 M
4 4 5.2 F
5 5 6.1 M
6 6 NA M
这篇关于读取水平组织的CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!