【问题标题】:How to use a script on multiple excel working sheets in r studio如何在 r studio 中的多个 excel 工作表上使用脚本
【发布时间】:2015-10-07 00:46:34
【问题描述】:

我有一个相当长的脚本,至少对我来说是这样,但我是 R 的新手。

看起来像这样

#DATA IS LOADED
library(xlsx)
df=read.xlsx2("/Users/emiliemariafalkkallenbach/Documents/Norsk_Institut_for_vandforskning/Chase_DK.xlsx",sheetIndex=1)
df[df==""]<- NA 
df=data.matrix(df)
df=data.frame(df)
attach(df)
Phyto=df[,1:6]
Submerged=df[,7:12]
Benthic=df[13:18]


# PHTYOPLANKTON 
#making of boundaries 
a<-(ifelse(P_Resp<=0,(1-P_Acdev),1/(1+P_Acdev)))
b=(0.95+a)/2
c=2*a-b
d=2*c-a
#Weighted boundaries 
e=0.95*P_weight
e=sum(e)
f=b*P_weight
f=sum(f)
g=a*P_weight
g=sum(g)
h=c*P_weight
g=sum(g)
i=d*P_weight
i=sum(i)
#EQR weighted 
j=P_EQR*P_weight
j=sum(j)
#complete
l <- ifelse(is.na(rowSums(Phyto)),1,0)
m=sum(l)


#SUBMERGED VEGETATION
#Overall assesment 
y=ifelse(m<=0,j,"")
z=ifelse(y!="NA",0,(ifelse(y>f,1,(ifelse(y>g,2,(ifelse(y>h,3,(ifelse(y>i,4,5)))))))))


#making of boundaries 
a.sub<-(ifelse(S_Resp<=0,(1-S_Acdev),1/(1+S_Acdev)))
a.sub=sum(a.sub)
b.sub=(0.95+a.sub)/2
b.sub=sum(b.sub)
c.sub=2*a.sub-b.sub
c.sub=sum(c.sub)
d.sub=2*c.sub-a.sub
d.sub=sum(d.sub)
#Weighted boundaries 
e.sub=0.95*S_weight
e.sub=sum(e)
f.sub=b.sub*S_weight
f.sub=sum(f)
g.sub=a.sub*S_weight
g.sub=sum(g)
h.sub=c.sub*S_weight
h.sub=sum(h)
i.sub=d.sub*S_weight
i.sub=sum(i)
#EQR.sub weighted 
j.sub=S_EQR*S_weight
j.sub=sum(j.sub)
#complete.sub
l.sub <- ifelse(is.na(rowSums(Submerged)),1,0)
m.sub=sum(l.sub)
#Overall Assesment
q.sub=m.sub*0.75
y.sub=ifelse(m.sub<=0,j.sub,"")
z.sub=ifelse(y.sub!="NA",(ifelse(y.sub>f.sub,1,(ifelse(y.sub>g.sub,2,(ifelse(y.sub>h.sub,3,(ifelse(y.sub>i.sub,4,5)))))))),0)

BENTHIC INVERTEBRATES
#making of boundaries 
a.ben<-(ifelse(B_Resp<=0,(1-B_Acdev),1/(1+B_Acdev)))
b.ben=(0.95+a.ben)/2
c.ben=2*a.ben-b.ben
d.ben=2*c.ben-a.ben
#Weighted boundaries 
e.ben=0.95*B_weight
e.ben=sum(e.ben)
f.ben=b.ben*B_weight
f.ben=sum(f.ben)
g.ben=a.ben*B_weight
g.ben=(sum(g.ben))
h.ben=c.ben*B_weight
h.ben=sum(h.ben)
i.ben=d.ben*B_weight
i.ben=sum(i.ben)
#EQR weighted 
j.ben=B_EQR*B_weight
#Complete
l.ben <- ifelse(is.na(rowSums(Benthic)),1,0)
m.ben=sum(l.sub)
#ChkAccDev
n.ben=ifelse(B_Resp>0,0.53,1.1)
o.ben=ifelse(B_Acdev<0.15,-1,0)
p.ben=ifelse(B_Acdev>n.ben,1,o.ben)
#Overall Assesment
q.ben=m.ben*0.75
y.ben=ifelse(m.ben<1,j.ben,"")
z.ben=ifelse(y.ben!="NA",ifelse(y.ben>f.ben,1,(ifelse(y.ben>g.ben,2,(ifelse(y.ben>h.ben,3,(ifelse(y.ben>i.ben,4,5))))))),0)

#Final assesment
Z=max(na.omit(c(z,z.sub,z.ben)))

#Overall assesment 
SCORE=(ifelse(Z<=1 && Z<2,"HIGH",(ifelse(Z>=2 && Z<3,"GOOD",(ifelse(Z>=3 && Z<4,"MODERATE",(ifelse(Z>=4 && Z<5,"BAD",(ifelse(Z>=5,"POOR","NA"))))))))))
SCORE


#Pie Chart 
library('plotrix')
Phyto=33.3
Submerged=33.3
Benthic=33.3
total=100
Slices1=c=(1)
total=100
iniR=0.2 
Slices1=c=(1)
pie(1, radius=iniR, init.angle=90, col=c('white'), border = NA, labels='Overall')
colors=c(ifelse(z<=1,"blue", ifelse(z<=2,"green",ifelse(z<=3,"yellow",ifelse(z<=4,"orange",ifelse(z<=5,"red","red"))))), ifelse(z.sub<=1,"blue", ifelse(z.sub<=2,"green", ifelse(z.sub<=3,"yellow",ifelse(z.sub<=4,"orange",ifelse(z.sub<=5,"red","red"))))),ifelse(z.ben<=1,"blue", ifelse(z.ben<=2,"green", ifelse(z.ben<=3, "yellow", ifelse(z.ben<=4,"orange",ifelse(z.ben<=5,"red","red"))))))
floating.pie(0,0,c(Phyto, Submerged, Benthic),radius=5*iniR, startpos=pi/2, col=colors,border=NA)
Slices1=c=(1)
total=100
iniR=0.2 # initial radius
colorst=c(ifelse(Z<=1,"blue", ifelse(Z<=2,"green",ifelse(Z<=3,"yellow",ifelse(Z<=4,"orange",ifelse(Z<=5,"red","red"))))))
floating.pie(0,0,c(total),radius=3*iniR, startpos=pi/2, col=colorst,border=NA)
angles=as.numeric(c(-10,75,80))
pie.labels(0,0,angles,c("Phyto","Submerged","Benthic"),radius=1, bg="white")
pie.labels(0,0,0,c("Overall Assessment"),radius=-0.3)

我想这并不重要,我的脚本是什么。 目前它只运行 excel 中的第一张工作表,但我有几个并且想在所有这些工作表上运行它们。

结果应该是这样的表格

             z      z.sub     z.ben    Z       Pie-chart (only if possible)

Sheet 1      0        NA        NA    High
Sheet 2     ...       ...       ...   ...
Sheet 3     ...       ...       ...   ... 

如果这是一个普通的问题,我很抱歉!

希望有人能帮忙

谢谢!

【问题讨论】:

    标签: r excel loops spreadsheet


    【解决方案1】:

    阅读 xls 的更好方法是库 readxl

    # remove "#" if you don't have these libraries installed already
    # install.packages("readxl")  # faster excel reader
    # install.packages("data.table")  # faster everything, in this case rbindlist
    
    library(readxl)
    library(data.table)
    
    sheets = 1:5  # index numbers or names of the sheets you want to read
    
    readmysheets = function(sheets) {
      df = read_excel(file="myexcel.xls", sheets)
    }
    
    myfiles = lapply(sheets, readmysheets)  # apply the indices/names on the readmysheets function
    # you now have a list:
    str(myfiles)
    
    # bind the separate sheets together
    together = rbindlist(myfiles, fill = T)
    

    【讨论】:

    • 把文件放到脚本所在的地方就行了。然后编写 dir() 并检查是否看到脚本和 xls 文件。
    【解决方案2】:

    将脚本的主体包裹到一个“for”循环中。你有几个是这样做的,这里有两个。

    # pre-allocate an object to write to
    out <- matrix(rep(NA, numsheets * numcols), ncols = numcols))
    for (i in 1:nsheets) {
      df=read.xlsx2(".../Chase_DK.xlsx", sheetIndex = i)
      #... do calculations
      out[, i] <- c(z, z.sub, z.ben, Z)
    
    }
    
    # second way, no need to pre-allocate anything
    sapply(1:nsheets, FUN = function(x) {
      df=read.xlsx2(".../Chase_DK.xlsx", sheetIndex = x)
      #... do calculations
      out <- c(z, z.sub, z.ben, Z) # specify what you wish the function to return
      return(out) # sapply will try to simplify the combined result on its own
    })
    

    【讨论】:

    • 感谢您的回复。由于某种原因它不起作用。我使用了您建议的第二种解决方案,但在 'y=ifelse(mError: unexpected string constant in: "#Overall assesment y= ifelse(m 而且它是否带有错误说找不到对象 e,f,g,h,i
    • 如果使用你的第一个建议,它会在第一行之后给出一个错误,说有太多 ")" 。如果我删除其中一个,它会显示“矩阵错误(rep(NA,28 * 18),ncols = numcols):未使用的参数(ncols = numcols)”
    • @EmilieK 请花时间制作一个可重复的示例。这样,我们将能够为您提供您所追求的确切行为。比如我在python chatroom问了一个问题,有一个可复现的例子,10秒就得到了答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    • 2020-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-09
    相关资源
    最近更新 更多