rm(list=ls())
library(stringr)
library(stringi)
factor_to_number <- function(strings){
strings <- as.character(strings)
result <- array(NA,dim=c(length(strings)))
tag <- array(T,dim=c(length(strings)))
for (i in 1:length(strings)){
if (!is.na(strings[i])){
number_of_dots <- as.numeric(stri_count(strings[i],regex = '\\.'))
if(number_of_dots==0){
#message("There's ",number_of_dots," dot(s) in the ",i," th number")
if (stri_count(strings[i],regex = '\\d') == nchar(strings[i])){
result[i] <- as.numeric(strings[i])
}
else {
message("Something unknow happening,please check the ",i,"th number")
tag[i] <- F
}
}
else{
if (number_of_dots >= 1){
#message("There's ",number_of_dots," dot(s) in the ",i," th number")
first_dot <- stri_locate_first(strings[i],regex = "\\.")[[1]][1]
string_before <- substr(strings[i],1,first_dot-1)
string_after <- substr(strings[i],first_dot+1,nchar(strings[i]))
if (stri_count(string_before,regex = '\\d') == nchar(string_before)){
num_before <- (string_before)
if (stri_count(string_after,regex = '\\d') == nchar(string_after)){
num_after <- (string_after)
result[i] <- as.numeric(paste(num_before,".",num_after,sep=""))
}
else{
first_not_number <- stri_locate_first(string_after,regex = "\\D")[[1]][1]
num_after <- substr(string_after,1,first_not_number-1)
result[i] <- as.numeric(paste(num_before,".",num_after,sep=""))
}
}
else {
message("Something unknow happening,please check the ",i,"th number")
tag[i] <- F
next
}
}
else {
message("Something unknow happening,please check the ",i,"th number")
tag[i] <- F
next
}
}
}
else {
#message("The ", i," th number is NA!" )
next
}
}
result_data <- data.frame(origin = strings,
result = result,
tag = tag)
return(result_data)
}
x<- read.csv("prcp.csv",header = T)
x1 <- x$Precipitation
y <-factor_to_number(x1)