像香港大井指出,你们的田地,由“;”分隔,而不是“”。功能read.csv具有默认值sep =“,”而read.csv2有默认值sep =“;”。如果我理解正确,您的字段作者和关键字由','分隔,您希望将它们分开。
我不认为你可以有项目在列作者和关键词在data.frame列表类型,作为data.frame的列不能是列表。如果给一个data.frame一个列表,它将被分解到它的列组件。在你的情况下,将无法正常工作,会有不同数量的作者和/或关键字:
# Works
data.frame(a=list(first=1:3, second=letters[1:3]), b=list(first=4:6, second=LETTERS[1:3]))
# a.first a.second b.first b.second
#1 1 a 4 A
#2 2 b 5 B
#3 3 c 6 C
# Does not work
data.frame(a=list(first=1:3, second=letters[1:2]), b=list(first=4:6, second=LETTERS[1:6]))
#Error in data.frame(first = 1:3, second = c("a", "b"), check.names = FALSE, :
# arguments imply differing number of rows: 3, 2
但由于列表可能包含列表,你可以尝试下破该数据帧这样的方式。 '例子的内容。TXT':
ID;Year;Title;Authors;Keywords;
1;2013;Towards Dynamic Non-obtrusive Health Monitoring Based on SOA and Cloud;Mohammed Serhani, Abdelghani Benharret, Erlabi Badidi;E-health, Diseases, Monitoring, Prevention, SOA, Cloud, Platform, m-tech;
2;1234;Title2;Author1, Author2;Key1, Key2, Key3;
3;5678;Title3;Author3, Author4, Author5;Key1, Key2, Key4;
下面是如何做到这一点的一个示例:
x <- scan("example.txt", what="", sep="\n", strip.white=TRUE)
y <- strsplit(x, ";")
# Leave out the header
dat <- y[-1]
# Apply a function to every element inside the highest level list
dat <- lapply(dat,
FUN=function(x) {
# Splits in authors and keywords list
ret <- strsplit(x, ",");
# Remove leading and trailing whitespace
ret <- lapply(ret, FUN=function(z) gsub("(^ +)|(+$)", "", z));
# Assign names to all the fields
names(ret)<-unlist(y[1]);
ret
}
)
输出:
> str(dat)
List of 3
$ :List of 5
..$ ID : chr "1"
..$ Year : chr "2013"
..$ Title : chr "Towards Dynamic Non-obtrusive Health Monitoring Based on SOA and Cloud"
..$ Authors : chr [1:3] "Mohammed Serhani" "Abdelghani Benharret" "Erlabi Badidi"
..$ Keywords: chr [1:8] "E-health" "Diseases" "Monitoring" "Prevention" ...
$ :List of 5
..$ ID : chr "2"
..$ Year : chr "1234"
..$ Title : chr "Title2"
..$ Authors : chr [1:2] "Author1" "Author2"
..$ Keywords: chr [1:3] "Key1" "Key2" "Key3"
$ :List of 5
..$ ID : chr "3"
..$ Year : chr "5678"
..$ Title : chr "Title3"
..$ Authors : chr [1:3] "Author3" "Author4" "Author5"
..$ Keywords: chr [1:3] "Key1" "Key2" "Key4"
# Keywords of first item
> dat[[1]]$Keywords
[1] "E-health" "Diseases" "Monitoring" "Prevention" "SOA"
[6] "Cloud" "Platform" "m-tech"
# Title of second item
> dat[[2]][[3]]
[1] "Title2"
# Traveling inside the list of lists, accessing the very last data element
> lastitem <- length(dat)
> lastfield <- length(dat[[lastitem]])
> lastkey <- length(dat[[lastitem]][[lastfield]])
> dat[[lastitem]][[lastfield]][[lastkey]]
[1] "Key4"
通知列表的,该目录可以存储在数据的低效的方式R,所以如果你有很多数据,你可能想要转向更高效的方法,例如关系数据库结构,其中访问密钥是您的ID,假设它是唯一的。
在许多国家/地区,逗号用作小数点分隔符,因此分号用于csv文件(是的,它们仍称为csv文件)作为列分隔符。 'read.table'起作用,但这些文件也有一个'read.csv2'。 –
@JanvanderLaan - '在很多国家......'据我所知,只有荷兰人使用这个惯例,这是我*讨厌*使用荷兰Excel版本的原因之一,特别是当与拥有国际化的人合作时版。 +1提到'read.csv2'! – nluigi
@nluigi有更多国家使用逗号作为小数点分隔符(可能不是中国和印度人使用的时间段)。请参阅https://en.wikipedia.org/wiki/Decimal_mark#Countries_using_Arabic_numerals_with_decimal_comma。我不知道这些国家的电子表格在做什么。但是我同意,excel的行为依赖于语言环境这一事实很令人讨厌。 –