How do I assign a dataframe name dynamically

I have the code below where I am trying to dynamically make dataframes like
zone1
zone2 etc basically I need to know how to make zonename be taken litterally on the line
zonename<-data %>% filter(zone==numzone)

data<-read_excel(filename)
numzone<-nrow(unique(data[c("zone")]))

for(i in 1:numzone) 
{
 zonename<-paste("Zone",numzone, sep = "")
 zonename
 zonename<-data %>% filter(zone==numzone) 
}
1 Like
assign(zonename,data %>% filter(zone==numzone) )
1 Like

I would highly recommend that you, rather than clutter your workspace with data frames, hold them in a list. You can do so by just a slight alteration to your code:

data<-read_excel(filename)
numzone<-nrow(unique(data[c("zone")]))

zones = list()
for(i in 1:numzone) 
{
 zonename<-paste("Zone",numzone, sep = "")
 zonename
 zones[[zonename]]<-data %>% filter(zone==numzone) 
}

It seems like you are looking to iterate over the subsets of data as defined by your variable zone. If so, try to keep the data in one data frame and then look into applying group_by and/or map-functions.

2 Likes

Thanks! nirgrahamuk that did the trick!

hmmm yes that might be a better Idea for me thanks Leon!

hey Leon
I think you method works best for me and your example surely works now my followup question.
data is a dataframe

so I extended your idea to this
where I get this error

Error in loc[[locis]] <- zones[[zonename]] %>% filter(location == locis) :
invalid subscript type 'list'

on this line:
loc[[locis]]<-zones[[zonename]] %>% filter(location==locis)

for some reason loc which is a list of dataframes is causing the issue...any ideas?


zones = list()
loc = list()
zonesReport = list()
locReport = list()
zonesAnova = list()
locAnova = list()

x<-data %>% filter(zone==1) 
ulocx<-unique(x[c("location")])
numulocx<-nrow(unique(x[c("location")]))
loc[j]<-data %>% filter(location==ulocx[1]) 

ulocx[2,1]

for(i in 1:numzone) 
{
 zonename<-paste("Zone",i, sep = "")
 zones[[zonename]]<-data %>% filter(zone==i) 
 numlocalhere<-nrow(unique(zones[[zonename]][c("location")]))
 localhere<-unique(zones[[zonename]][c("location")])
 
 print(zonename)
 print(i)
 
 j=0
 for (z in 1:numlocalhere) 
 { 
   j=j+1
   locis=ulocx[z,1]
   locname<-paste("Zone",i,"-",locis, sep = "")
   loc[[locis]]<-zones[[zonename]] %>% filter(location==locis)
   locReport[[locis]] <- loc[[locis]] %>% 
     group_by(name) %>% 
     summarise(AVG = mean(yield_kgha , na.rm = TRUE),n=n(),StdDev=sd(yield_kgha , na.rm = TRUE),cvpct=((sd(yield_kgha , na.rm = TRUE)/mean(yield_kgha , na.rm = TRUE))*100)) 
 
  locAnova[[locis]] <- loc[[locis]] %>% group_by(zone,location,name)
   modeldw1<-aov(yield_kgha~name+bloc,data=locAnova[z])

   order<-loc[z] %>% group_by(name) %>% summarize(n=n())
   max(order$n)
   dfx<-tail(summary(modeldw1)[[1]]$`Df`, n=1)
   msx<-tail(summary(modeldw1)[[1]]$`Mean Sq`, n=1)
   meanis=mean(loc[z]$yield_kgha)
   stddevis=sd(loc[z]$yield_kgha)
   cvpctis=(stddevis/meanis)*100
 
   lsdis<-abs(qt(0.05/2,dfx*1))*sqrt(msx*2/(33))
   write.csv(locReport[[locis]],paste(csvdir,"\\",locname,".csv", sep = ""), row.names = TRUE)
   write.table(paste("lsd at 5%",  lsdis), paste(csvdir,"\\data",i,".csv", sep = ""), col.names = !file.exists(filename), append = T)
   write.table(paste("CV%:",  cvpctis), paste(csvdir,"\\data",i,".csv", sep = ""), col.names = !file.exists(filename), append = T)
   #write.table(paste("# Locations:",  numlocalhere), paste(csvdir,"\\data",i,".csv", sep = ""), col.names = !file.exists(filename), append = T)
 }
}

It's not entirely clear to me, what you are trying to achieve. Your code seems a mix of different approaches. Perhaps if you supplied a bit of input data, a description and illustrated expected output?

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.