Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Like Hong Ooi pointed out, your fields are separated by ';', not ','. Function <em>read.csv</em> has default value <em>sep=","</em> while <em>read.csv2</em> has default <em>sep=";"</em>. If I understood correctly, your fields <em>Authors</em> and <em>Keywords</em> are separated by ',' and you wish to separate these as well. </p> <p>I do not think you could have a list type of items in columns <em>Authors</em> and <em>Keywords</em> in a data.frame, as the column of a data.frame cannot be a list. If a list is given to a data.frame, it is broken down to its column components. In your case it will not work as there will be a varying number of authors and/or keywords:</p> <pre><code># 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 </code></pre> <p>But since a list may contain lists, you could try break the data frame down to such. Contents of 'example.txt':</p> <pre><code>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; </code></pre> <p>Here is an example of how to do it:</p> <pre><code>x &lt;- scan("example.txt", what="", sep="\n", strip.white=TRUE) y &lt;- strsplit(x, ";") # Leave out the header dat &lt;- y[-1] # Apply a function to every element inside the highest level list dat &lt;- lapply(dat, FUN=function(x) { # Splits in authors and keywords list ret &lt;- strsplit(x, ","); # Remove leading and trailing whitespace ret &lt;- lapply(ret, FUN=function(z) gsub("(^ +)|( +$)", "", z)); # Assign names to all the fields names(ret)&lt;-unlist(y[1]); ret } ) </code></pre> <p>Output:</p> <pre><code>&gt; 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 &gt; dat[[1]]$Keywords [1] "E-health" "Diseases" "Monitoring" "Prevention" "SOA" [6] "Cloud" "Platform" "m-tech" # Title of second item &gt; dat[[2]][[3]] [1] "Title2" # Traveling inside the list of lists, accessing the very last data element &gt; lastitem &lt;- length(dat) &gt; lastfield &lt;- length(dat[[lastitem]]) &gt; lastkey &lt;- length(dat[[lastitem]][[lastfield]]) &gt; dat[[lastitem]][[lastfield]][[lastkey]] [1] "Key4" </code></pre> <p>Notice that lists of lists may be an inefficient way to store data in R, so if you have lots of data you may want to move to a more efficient method, e.g. relational database structure where the access key is your ID, assuming it is unique.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload