2013-05-22 108 views
0

我对R还是比较陌生,可能已经完全搞乱了数据框架的概念。使用嵌入式列表从CSV文件创建数据框

但我有以下格式的CSV文件:

ID;Year;Title;Authors;Keywords; 

如果作者和关键词应该是一个字符串列表。例如。

1; 2013;向基于SOA和云动态非侵扰健康监测;穆罕默德Serhani,Abdelghani Benharret,Erlabi Badidi; E-健康,疾病监测,预防,SOA,云计算,平台,间高科技;

有没有办法将这个csv文件读入R中,这样作者和关键字的数据框列就被构建为列表清单了?这是否需要我以特定的方式格式化csv文件?

读取用下列选项

articles <- read.csv(file="ls.csv",head=TRUE,sep=";",stringsAsFactors=F) 

该CSV产率作者式柱作为含有字符实例的列表。但我想要实现的是获取作者列中每个字段的字符列表。

回答

1

你是说你的文件包含五个用分号隔开的变量(ID,年份,标题,作者,关键字)吗?然后,根据定义,它不是一个csv文件!请记住,csv代表逗号-分隔值。有人把它命名为这样搞砸了。

您可以阅读使用read.table任意分隔的数据:

articles <- read.table("ls.csv", header=TRUE, sep=";", stringsAsFactors=FALSE) 
+3

在许多国家/地区,逗号用作小数点分隔符,因此分号用于csv文件(是的,它们仍称为csv文件)作为列分隔符。 'read.table'起作用,但这些文件也有一个'read.csv2'。 –

+0

@JanvanderLaan - '在很多国家......'据我所知,只有荷兰人使用这个惯例,这是我*讨厌*使用荷兰Excel版本的原因之一,特别是当与拥有国际化的人合作时版。 +1提到'read.csv2'! – nluigi

+1

@nluigi有更多国家使用逗号作为小数点分隔符(可能不是中国和印度人使用的时间段)。请参阅https://en.wikipedia.org/wiki/Decimal_mark#Countries_using_Arabic_numerals_with_decimal_comma。我不知道这些国家的电子表格在做什么。但是我同意,excel的行为依赖于语言环境这一事实很令人讨厌。 –

0

像香港大井指出,你们的田地,由“;”分隔,而不是“”。功能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,假设它是唯一的。