就像 Hong Ooi 指出的那样,您的字段由“;”分隔,而不是“,”。函数read.csv具有默认值sep=","而read.csv2具有默认值sep=";" . 如果我理解正确,您的字段作者和关键字由“,”分隔,您也希望将它们分开。
我认为您不能在 data.frame 中的Authors和Keywords列中拥有列表类型的项目,因为 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
但由于列表可能包含列表,您可以尝试将数据框分解为此类。“example.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 的关系数据库结构,假设它是唯一的.