Reputation: 13
I'm a newbie in R. I have a 24MB CSV file. Reads this into RStudio on my MacBook Air with OS Yoswmite, 4GB RAM. R version 3.1.1 (2014-07-10). Viewing the contents of View (df) is OK. Attempting to apply filter. Do not get any hits. Attempting to cast from character to number. R replacing all charaters with NA in the column where the casting is done! What happens here? It seems that R can not read the contents of the cells. Is there anything about encoding? This is what I have done so fare: First a summary:
R Code:
eiendommer <- read.csv("eiendommer.csv", sep = ";", quote = "", encoding="UTF-8", stringsAsFactors = FALSE)
View(eiendommer)# I can view the content of the csv file
filtereiendommer <- filter(eiendommer, kommune == "0101")# no match
filtereiendom <- eiendommer [eiendommer$kommune == "0101",]#no match
utvalg <- eiendommer[160567:161934,]#manual selection of rows do work utvalgsortert <- arrange(utvalg, desc(jordbruksareal), desc(skogareal))# works
View(utvalgsortert)
##Try to transform columns from character to number.
transformedEiendom <- transform(sortertEiendom, jordbruksareal = as.numeric(jordbruksareal),
skogareal = as.numeric(skogareal) )
#This result in NA where it earlier was characters with lengt 1-3:"646", "18", "2"
Summary:
kommune X.gardsnr. X.bruksnr. X.festenr. bruksnavn jordbruksareal
Length:207554 Length:207554 Length:207554 Length:207554 Length:207554 Length:207554
Class :character Class :character Class :character Class :character Class :character Class :character
Mode :character Mode :character Mode :character Mode :character Mode :character Mode :character
X.annetareal. skogareal X.fulldyrket. X.overflatedyrket. X.innmarksbeite.
Length:207554 Length:207554 Length:207554 Length:207554 Length:207554
Class :character Class :character Class :character Class :character Class :character
Mode :character Mode :character Mode :character Mode :character Mode :character
Head:
head(eiendommer)
kommune X.gardsnr. X.bruksnr. X.festenr. bruksnavn jordbruksareal X.annetareal. skogareal X.fulldyrket.
1 "0101" "1" "1" "0" "PRESTEGÅRD" "0" "5" "0" "0"
2 "0101" "1" "6" "0" "MO" "8" "4" "7" "8"
3 "0101" "1" "9" "0" "BERG GÅRD" "415" "16" "39" "415"
4 "0101" "2" "1" "0" "BOBERG" "467" "22" "276" "463"
5 "0101" "4" "1" "0" "LUNDESTAD" "877" "62" "793" "837"
6 "0101" "4" "5" "0" "LEIREN" "74" "14" "165" "74"
Upvotes: 0
Views: 219
Reputation: 13
This caused my problem in the file:
;"BLOMSTERHAGEN\"";
Changing it to:
;"BLOMSTERHAGEN";
Fixed the problem. No I can read.csv like this:
eiendommer <- read.csv("eiendommer.csv", sep = ";", encoding="UTF-8", stringsAsFactors = FALSE)
Thanks
Upvotes: 0
Reputation: 927
One challenge you will have with Norwegian Kommune Nummer, is those starting with 0 like Halden "0101".
#Prepare Data
kommune = rep("0101", 6)
jordbruksareal<- c("5","4","16","22","62","14")
skogareal <- c("0","8","415","463","837", "74")
eiendommer <- cbind(kommune, jordbruksareal, skogareal)
eiendommer <- as.data.frame(cbind(kommune, jordbruksareal, skogareal), stringsAsFactors=FALSE)
#Transform into numeric
str(eiendommer) #All is Character
eiendommer$skogareal<-as.numeric(eiendommer$skogareal)
eiendommer$jordbruksareal<-as.numeric(eiendommer$jordbruksareal)
eiendommer$kommune<-as.numeric(eiendommer$kommune)
str(eiendommer) #All is numeric, but losing first zer0
#Make a filter
require(dplyr)
filterA <- filter(eiendommer, eiendommer$jordbruksareal == "4")
filter <- subset(eiendommer, eiendommer$kommune == 101)
#Treat Kommune Numbers
eiendommer$kommune <- formatC(eiendommer$kommune, digits = 0, format = "f", width = 4, flag = 0)
eiendommer$kommune <- sprintf("%04d",eiendommer$kommune)
str(eiendommer)
filter2 <- subset(eiendommer, eiendommer$kommune == "0101")
Hope this helps a little, Ha det bra!
Upvotes: 0
Reputation: 115465
It would appear that you have specified quote = ""
, where in fact you should have, and perhaps quote='"'
or simply the default value would work.
See the example below
d <- data.frame(x='a',y='"a"',stringsAsFactors=FALSE)
d
# x y
# 1 a "a"
For a regular character vector, print.data.frame
will not enclose it in "
Upvotes: 2