0

I try to create descriptive statistics for different groups of refugees. I want to use a for loop which creates the same tables always for a certain sub sample of my refugees data base. I create multiple sub groups for different countries using a list.

I tried:

all <- refugees
syr <- subset(refugees[refugees$nation_vorher == "MSYR"])
all$arrival_y <- format(as.Date(all$arrival), format("%Y"))
all$gebj <- as.integer(all$gebj)
syr$arrival_y <- format(as.Date(syr$arrival), format("%Y"))
syr$gebj <- as.integer(syr$gebj)


countries = list(all, syr)

results <- vector(mode = "integer", length = length(countries))

for (i in 1:length(countries)){
  refugees <- countries[i]
  
 
  
  arr2007 <- subset(refugees[refugees$arrival_y == "2007" & refugees$gebj <= 1992])
  arr2011 <- subset(refugees[refugees$arrival_y == "2011" & refugees$gebj <= 1996])
  arr2016 <- subset(refugees[refugees$arrival_y == "2016" & refugees$gebj <= 2001])
  arr2020 <- subset(refugees[refugees$arrival_y == "2020" & refugees$gebj <= 2005]) 
}

... more code below

The code works perfectly if I run it without the loop. However, when I run the loop I get the following Error: Error in subset.default(refugees[refugees$arrival_y == "2007" & refugees$gebj <= : argument "subset" is missing, with no default

I hope you can help me, thanks!

EDIT some lines of data:

     ID arrival      nation_vorher    bez_stsl gebj gesl arrival_y
     1    2005-01-01          MSYR     Serbien 1955    F      2007
     2    2009-02-23          MAFG        <NA> 1953    M      2009
     3    2002-08-27          MSYR     Serbien 1976    M      2016
     4    2004-07-01          <NA>        <NA> 1943    F      2016
     5    2005-05-30           MKS      Kosovo 1977    F      2020
econ9595
  • 5
  • 2
  • It would be helpful if you could provide a sample dataset using `dput(x)`, refer https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Nad Pat Mar 29 '22 at 18:21
  • I added some lines of manipulated data. I can't upload any of my real data. – econ9595 Mar 29 '22 at 18:48
  • Manipulated data is fine, but please post it using `dput` so we can copy-paste it – Dubukay Mar 29 '22 at 19:02
  • Try `refugees <- countries[[i]]`, but beyond that I would suggest posting more data (note: you've not indicated what the data you posted is called in your example code) and your overall goals. It looks like there's lots of room for improvement here, and we'd be happy to help, just need a bit more info :) – Skaqqs Apr 01 '22 at 20:21

0 Answers0