function in R to change numbers into time format w

2019-09-21 14:38发布

This question already has an answer here:

I want to change 2 , 3 and 4 digits numbers in excel file into hour and minute format without dots between the numbers. for example I have 83 and 114 and 1159 . I want to write a function in r that can consider the first two digits from the right as minutes and the third and forth as hour , so for 114 the function should consider 14 as minutes then 1 as an hour and the same for other numbers.

for clarification my question is how to change 83, 141, to hour and minutes without dots 141= 0141 and 83 = 0123 and so on

thanks Salem

标签: r excel
1条回答
萌系小妹纸
2楼-- · 2019-09-21 15:37

This is a very specific function to write, and I do not know if this is the best way to do this, but this will read any two digits to the right as min and any two to the left as hrs. If the mins is over 60 it will add it to the overall hours.

extractTime.c<-function(list_char){

Test_Char<-as.character(list_char)

a1 <- as.matrix(Test_Char) ;
a2 <- strsplit(a1, "") ;


Data_List<-list()
List_Length<-length(a2)

for(i in 1:List_Length){

Char_Length<-length(a2[[i]])

if(Char_Length==2){
min1<-a2[[i]][1]
min2<-a2[[i]][2]
MIN<-paste(min1,min2, sep="")

MIN<-as.numeric(MIN)
HR=0

if(MIN/60 > 1){
  Tot_Hrs<-MIN/60
  Tot_Hrs_Char<-as.character(Tot_Hrs)

  Percent_Hr<-regmatches(Tot_Hrs_Char, gregexpr("\\W\\d\\d"
                                      , Tot_Hrs_Char, perl=TRUE))
  Percent_Hr<-as.numeric(Percent_Hr)

  MIN<-Percent_Hr*60
  MIN<-round(MIN)

  HR<-regmatches(Tot_Hrs_Char, gregexpr("\\d+\\W"
                                        , Tot_Hrs_Char, perl=TRUE))
  HR<-regmatches(HR,gregexpr("\\d+", HR, perl=T))

  HR<-as.numeric(HR)

  }else if(MIN/60==1){
   HR=HR+1
   MIN=0

 }else{
  HR=0

 }

Time<-data.frame(HR,MIN)

Data_List[[i]] <-Time


}else if(Char_Length==3){
hr1<-a2[[i]][1]
min1<-a2[[i]][2]
min2<-a2[[i]][3]
MIN<-paste(min1,min2,sep="")

hr1<-as.numeric(hr1)
MIN<-as.numeric(MIN)
if(MIN/60 > 1){
  Tot_Hrs<-MIN/60
  Tot_Hrs_Char<-as.character(Tot_Hrs)

  Percent_Hr<-regmatches(Tot_Hrs_Char, gregexpr("\\W\\d\\d"
                                                , Tot_Hrs_Char, perl=TRUE))
  Percent_Hr<-as.numeric(Percent_Hr)

  MIN<-Percent_Hr*60
  MIN<-round(MIN)

  HR<-regmatches(Tot_Hrs_Char, gregexpr("\\d+\\W"
                                        , Tot_Hrs_Char, perl=TRUE))
  HR<-regmatches(HR,gregexpr("\\d+", HR, perl=T))

  HR<-as.numeric(HR)
  hr1<-as.numeric(hr1)
  HR<-(HR+hr1)

}else if(MIN/60==1){
  HR=hr1+1
  MIN=0   

}else{
  HR<-hr1
}

Time<-data.frame(HR,MIN)
Data_List[[i]] <-Time

}else if (Char_Length==4){
hr1<-a2[[i]][1]
hr2<-a2[[i]][2]
min1<-a2[[i]][3]
min2<-a2[[i]][4]
MIN<-paste(min1,min2, sep="")
HROG<-paste(hr1,hr2, sep="")

MIN<-as.numeric(MIN)

if(MIN/60 > 1){
  Tot_Hrs<-MIN/60
  Tot_Hrs_Char<-as.character(Tot_Hrs)

  Percent_Hr<-regmatches(Tot_Hrs_Char, gregexpr("\\W\\d\\d"
                                                , Tot_Hrs_Char, perl=TRUE))
  Percent_Hr<-as.numeric(Percent_Hr)

  MIN<-Percent_Hr*60
  MIN<-round(MIN)

  HR<-regmatches(Tot_Hrs_Char, gregexpr("\\d+\\W"
                                        , Tot_Hrs_Char, perl=TRUE))
  HR<-regmatches(HR,gregexpr("\\d+", HR, perl=T))

  HR<-as.numeric(HR)
  HROG<-as.numeric(HROG)
  HR<-(HR+HROG)

}else if(MIN/60==1){
  HR=HROG+1
  MIN=0   

}else{
  HR<-HROG

}

Time<-data.frame(HR,MIN)
Data_List[[i]] <-Time


}else{}



}
Fin_DF<-do.call(rbind.data.frame, Data_List)
return(Fin_DF)
}

Then we call the function written above...

Test<-c(183,1225,12,1001,260)
A<-extractTime.c(Test)

> A
  HR MIN
1  2  23
2 12  25
3  0  12
4 10   1
5  3   0
查看更多
登录 后发表回答