Web EndMemo

R Read.csv Example


read.csv() function reads a file into data frame. CSV file can be comma delimited or tab or any other delimiter specified by parameter "sep=". If the parameter "header=" is "TRUE", then the first row will be treated as the row names.

read.csv(file, header = FALSE, sep = ",", quote = "\"",
           dec = ".", fill = TRUE, comment.char = "", ...)
read.csv2(file, header = TRUE, sep = ";", quote = "\"",
          dec = ",", fill = TRUE, comment.char = "", ...)		   

• file: file name
• header: 1st line as header or not, logical
• sep: field separator
• quote: quoting characters
...

The difference between read.csv and read.csv2 is the default field seperator, as "," and ";" respectively.

Following is a csv file example:


> x <- read.csv("readcsv.csv", header=T, dec=".",sep="\t")
> typeof(x)
[1] "list"

> is.data.frame(x)
[1] TRUE


We need to summarize how many 0, 1 and 2 are there in sample t1, t2 ... t8. Following R code can be used to handle the job:

R source file:
x <- read.csv("readcsv.csv", header=T, dec=".",sep="\t")
xC = ncol(x)
xR = nrow(x)
ll <- data.frame(matrix(data = 0, nrow=3, ncol=8,byrow=T))
colnames(ll) <- names(x[,2:xC])
rownames(ll) <- c(0,1,2)
for (c in 2:xC) 
{
    for (r in 1:xR)
    {
         if (x[r,c]==0) 
         {
			ll[1,c-1] = ll[1,c-1] + 1;
         }
         else if (x[r,c]==1) 
         {
			ll[2,c-1] = ll[2,c-1] + 1;
         }
         else if (x[r,c]==2) 
         {
			ll[3,c-1] = ll[3,c-1] + 1;
         }
    }
}
print(ll)


The result is:
  t1 t2 t3 t4 t5 t6 t7 t8
0  9 10 11  5  5  7 10  8
1  7  3  6 11 10  8  6  8
2  4  7  3  4  5  5  4  4