像香港大井指出,你們的田地,由「;」分隔,而不是「」。功能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的行爲依賴於語言環境這一事實很令人討厭。 –