我对R还是很陌生的,可能已经把数据帧的概念搞砸了。
但是我有一个csv文件,格式如下:
ID;Year;Title;Authors;Keywords;其中作者和关键字应该是字符串的列表。例如。
1;2013年;基于SOA和Cloud的动态非突发性健康监测;Mohammed Serhani、Abdelghani Benharret、Erlabi Badidi;E-保健、疾病、监测、预防、SOA、云、平台、m-技术;
是否有方法将这个csv文件读入R,以便将作者和关键字的数据帧列构建为列表列表?这是否需要我以一种特定的方式格式化csv文件?
使用以下选项读取csv
articles <- read.csv(file="ls.csv",head=TRUE,sep=";",stringsAsFactors=F)生成包含字符实例的列表。但是我想要实现的是,在作者列中的每个字段中都有一个字符列表。
发布于 2013-05-22 09:00:37
您是说您的文件包含由分号分隔的五个变量(ID、年份、标题、作者、关键字)?然后,根据定义,它不是csv文件!请记住,csv代表逗号分隔的值。有人把它命名成这样搞砸了。
您可以使用read.table读取任意分隔的数据。
articles <- read.table("ls.csv", header=TRUE, sep=";", stringsAsFactors=FALSE)发布于 2013-05-22 10:05:45
正如洪欧所指出的,你的字段是用“;”而不是“,”分隔的。函数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但是,由于列表可能包含列表,所以可以尝试将数据帧分解为这样的列表。“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 (假设它是唯一的)的关系数据库结构。
https://stackoverflow.com/questions/16687210
复制相似问题