将多种格式应用于R中data.frame中的列

时间:2019-02-04 12:35:41

标签: r excel

我在excel中有一个数据集,我将其导入到R作为数据框。经过一些操作后,我将同一数据框的新版本重新导入Excel。现在,在某些列中有各种字符值。这使整列返回字符值(包括我想保留为数字格式的数字值)。

我想保持数字格式的数字值和字符格式的字符值。为此,我编写了两个for循环,这些循环必不可少,它将所有字符值都移出数据帧,并且一旦通过col_types函数中的read_excel参数将列转换为数字格式(将替换为带有NA的字符值),然后放回excel,R通过writeData将所有字​​符值写入Excel。下面的代码:

library(readxl) #To read files
library(openxlsx) # To save data

rev<-as.data.frame(read_excel(choose.files(),sheet="Data",range="A7:FA400"))

t<-list() #list to contain row positions for each character value
colA<-c() #vector to contain col positions for each character value
for(i in 1:ncol(rev)){
  dummy<-try(any(rev[,i]=="DNR"),silent=T)
  if(inherits(dummy,"try-error")){# In case there is an error, I am skipping that column and go to the next one
    print(paste0("col ",i,": There was an error"))# Letting me know where error was
  }else{ 
  vecA<-which(rev[,i]=="DNR")#checking where chr values are
  t<-append(t,list(vecA))#Noting row positions
  colA<-append(colA,i)#Noting col positions
}
}
vec2<-c()
for(i in 1:length(t)){# In case NA is returned, I would like to exclude them from my list of rows and my vector of columns
  if(!length(t[[i]])>0){
  vec2<-append(vec2,i)  
  }
}
t<-t[-vec2]
colA<-colA[-vec2]

rev2<-as.data.frame(read_excel(choose.files(),sheet="Incomp18wksProv",range="A7:FA400",na=c("NULL","NA", "#N/A",'-',''),col_types=c("text",rep("numeric",ncol(rev)-1))))
rev2<-rev2[rowSums(is.na(rev2))!=ncol(rev2),]
x<-"DNR" #Name of a chr value

wb<-loadWorkbook(choose.files())

for(i in 1:length(colA)){# Creating cell position to put chr values in
  for(j in 1:length(t[[i]])){
    x<-"DNR"
    writeData(wb,sheet="Sheet2",x,startCol = colA[i],startRow = t[[i]][j]+1)
  }
}
writeData(wb,sheet="Sheet1",rev)

saveWorkbook(wb,choose.files(),overwrite=TRUE)# Saving file

运行需要花费很多时间,所以我想知道是否有更好的方法来执行相同的任务。

虚拟数据框:data.frame(c(rep("Name",50)),c(rep(NA,50)),c(rep(1,45),"DNR","DNR",1,1,1),c("DNR","DNR",rep(1,48)))

谢谢!

0 个答案:

没有答案