Home > Net >  Basic for loop in R
Basic for loop in R

Time:12-15

I am trying to implement a for loop for a set of data-frames that I want to write to Excel

AU2<-intersect(A_CN_U$symbol,A_CP_U$symbol)
AD2<-intersect(A_CN_D$symbol,A_CP_D$symbol)
BU2<-intersect(B_CN_U$symbol,B_CP_U$symbol)
BD2<-intersect(B_CN_D$symbol,B_CP_D$symbol)
CU2<-intersect(C_CN_U$symbol,C_CP_U$symbol)
CD2<-intersect(C_CN_D$symbol,C_CP_D$symbol)

tot<- c(AU2, AD2, BU2, BD2, CU2, CD2)
for (i in tot){
  print(i)
  write_xlsx(i,"/Users/ABC/Desktop/Research/i.xlsx")
}

It returns this:

[1] "TMTC2"
Error in write_xlsx(i, "/Users/abkhan/Desktop/Research/Patel Meningioma/Smoker DEG/01RedoSubmissionMM/i.xlsx") : 
  Argument x must be a data frame or list of data frames

Outside my failed loop:

> AU2
 [1] "TMTC2"    "NPB"      "GALNT6"   "CDCA2"    "ABTB1"    "C12orf75" "GPR63"    NA         "ESR1"     "NPAS2"    "PLAGL1"   "C11orf45" "SYNE1"    "C16orf74"
[15] "S100A6"   "LOXL4"    "PLCL1"    "KLHL29"   "DTX4"     "ITGB5"    "BCAT1"    "CDKN2B"   "KANK4"    "S1PR2"    "AHR"      "STAMBPL1" "TRPM3"    "TMEM200A"
[29] "BASP1"    "AQP5"     "THBS2"    "ADRA1B"   "MGLL"     "RIMBP2"   "KCNN4"    "PROCR"    "MXRA5"    "CAV1"     "GALNT15"  "RIMS1"    "ELAVL4"   "COL4A6"  
[43] "FAM189A1" "AMH"      "DPP4"     "MEGF6"    "JPH3"     "POU5F1B"  "EVA1A"    "ABCC2"    "PTGES"    "CACNG8"   "ALK"      "VGLL3"    "TGM2"     "SLC9A2"  
[57] "LVRN"     "MEGF10"   "LMO3"     "PRPH"     "ATP2B2"   "SRPX2"    "LUM"      "SLC9A4"   "CDKN2A"   "LGR6"     "ALPK2"    "C6orf132" "FAP"      "ANKRD1"  
[71] "LTK"      "ASPN"     "SLC22A1"  "PPL"      "LYPD1"    "GPR39"    "DSC3"     "SOX11"    "NHLH2"    "KRT14"    "IGFL2"    "GDF6"    

Thanks!

Edit: I realized that my original variable (Au2, AD2 etc) were not data frames I converted them to dataframes and their names to reflect my mood.

Now I get:

> tot<- c(Atrash, Btrash, Ctrash)
> for (i in tot){
    print(i)
    write_xlsx(i,"/Users/abkhan/Desktop/Research/Patel Meningioma/Smoker DEG/01RedoSubmissionMM/i.xlsx")
    }
 [1] "TMTC2"    "NPB"      "GALNT6"   "CDCA2"    "ABTB1"    "C12orf75" "GPR63"    NA         "ESR1"     "NPAS2"    "PLAGL1"   "C11orf45" "SYNE1"    "C16orf74"
[15] "S100A6"   "LOXL4"    "PLCL1"    "KLHL29"   "DTX4"     "ITGB5"    "BCAT1"    "CDKN2B"   "KANK4"    "S1PR2"    "AHR"      "STAMBPL1" "TRPM3"    "TMEM200A"
[29] "BASP1"    "AQP5"     "THBS2"    "ADRA1B"   "MGLL"     "RIMBP2"   "KCNN4"    "PROCR"    "MXRA5"    "CAV1"     "GALNT15"  "RIMS1"    "ELAVL4"   "COL4A6"  
[43] "FAM189A1" "AMH"      "DPP4"     "MEGF6"    "JPH3"     "POU5F1B"  "EVA1A"    "ABCC2"    "PTGES"    "CACNG8"   "ALK"      "VGLL3"    "TGM2"     "SLC9A2"  
[57] "LVRN"     "MEGF10"   "LMO3"     "PRPH"     "ATP2B2"   "SRPX2"    "LUM"      "SLC9A4"   "CDKN2A"   "LGR6"     "ALPK2"    "C6orf132" "FAP"      "ANKRD1"  
[71] "LTK"      "ASPN"     "SLC22A1"  "PPL"      "LYPD1"    "GPR39"    "DSC3"     "SOX11"    "NHLH2"    "KRT14"    "IGFL2"    "GDF6"    
Error in write_xlsx(i, "/Users/ABC/Desktop/Research/i.xlsx") : 
  Argument x must be a data frame or list of data frames

CodePudding user response:

writexl take the first argument as a:

data frame or named list of data frames that will be sheets in the xlsx

So you are passing another type of data. You can create a list of your data.frames and iterate within the list to loop each data.frame, here an example:

df1 <- data.frame(a = 1)

df2 <- data.frame(b = 2)

list_of_dfs <- list(df1,df2)

n <- length(list_of_dfs)

for (i in 1:n){
  print(i)
  write.csv(list_of_dfs[[i]],file = paste0(i,".csv"),row.names = FALSE)
}

list.files(pattern = "*.csv")

[1] "1.csv" "2.csv"
  • Related