R read.csv()


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
More auguments see read.table().

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

read.csv() can also read from the clipboard. Let's select and copy some data from the Excel:



> z <- read.csv(file="clipboard", sep="\t", header=FALSE)
> colnames(z) <- c("city","latitude","longitude")
> z
                   city   latitude longitude
1   Fernando De Noronha  -3.844965 -32.40944
2  Governador Valadares -18.849852 -41.94927
3         Iguassu Falls -25.252089 -52.02154
4          Jacareacanga  -6.206604 -57.82447
5     Juazeiro Do Norte  -7.237234 -39.32218
6         Monte Dourado  -0.866667 -52.51667
7   Presidente Prudente -22.127193 -51.38517
8             Rio Verde -17.789523 -50.92043
9             Salvadore -12.970382 -38.51238
10             Sau Luiz  -2.530731 -44.30683
11            Trombetas  -1.488766 -56.39394


Visit read.table() for more information.
endmemo.com © 2024  | Terms of Use | Privacy | Home