[R] Column names shifted

arun smartpink111 at yahoo.com
Sun Aug 11 06:27:48 CEST 2013


Hi,
You may try:
dat1<- structure(list(Date.Time = c("C", "C", "C"), Unit = c(23L, 23L, 
22L), Value = c(207L, 82L, 894L)), .Names = c("Date.Time", "Unit", 
"Value"), class = "data.frame", row.names = c("27/06/13 02:35:01", 
"27/06/13 02:50:01", "27/06/13 03:05:01"))

dat2<-data.frame(Date.Time=row.names(dat1),Unit=dat1[,1],Value=as.numeric(paste(dat1[,2],sprintf("%03d",dat1[,3]),sep="."))
,stringsAsFactors=FALSE)
dat2
#          Date.Time Unit  Value
#1 27/06/13 02:35:01    C 23.207
#2 27/06/13 02:50:01    C 23.082
#3 27/06/13 03:05:01    C 22.894


#or
 lines1<-readLines("data1.csv")
dat2New<-read.table(text=gsub("(.*),(.*)$","\\1.\\2",lines1[-1]),sep=",",header=FALSE,stringsAsFactors=FALSE)
 colnames(dat2New)<-strsplit(lines1[1],",")[[1]]
 dat2New
#          Date/Time Unit  Value
#1 27/06/13 02:35:01    C 23.207
#2 27/06/13 02:50:01    C 23.082
#3 27/06/13 03:05:01    C 22.894
 colnames(dat2)[1]<- colnames(dat2New)[1]
 identical(dat2,dat2New)
#[1] TRUE

A.K.



Hi, 

I'm trying to import a csv file into R using read.csv. Here's the first part of the CSV: 

Date/Time,Unit,Value 
27/06/13 02:35:01,C,23,207 
27/06/13 02:50:01,C,23,082 
27/06/13 03:05:01,C,22,894 

When I import this to R using: 

data <- read.csv(file="", header =TRUE, skip=19) 

I get: 

                           Date.Time Unit Value 
27/06/13 02:35:01         C   23   207 
27/06/13 02:50:01         C   23    82 
27/06/13 03:05:01         C   22   894 

All the names are shifted to the right and the column with 
temperature(Value) is split in two, probable because R thinks the Value 
is two different columns since its in the form of 23,207 instead of 
23.207. Is there any way to make this right within R or do I have to 
manually change alle the Temperature values? 

Thanks!



More information about the R-help mailing list