What I want:
1804
1808
1812
etc...
years <- seq(1800,2020)
for (i in years){
i_div_400 <- i%%400
if (i_div_400 == 0 & (i%%4 == 0 && i%%100 != 0)){
write.table(i, "file.dat", append=TRUE, sep=",", quote=FALSE)
}
}
The reason why you get "x" in the output is because that is the default column names when using write.table
. If you don't want to include column name in the output use col.names = FALSE
and it will print only numbers in the output.
years <- seq(1800,2020)
for (i in years){
i_div_400 <- i%%400
if (i_div_400 == 0 || (i%%4 == 0 && i%%100 != 0)){
write.table(i, "file.dat", append=TRUE, sep=",",
row.names=FALSE, quote=FALSE, col.names = FALSE)
}
}
Also this can be written in a vectorised way without a loop.
write.table(years[years %% 400 == 0 | (years %% 4 == 0 & years %%100 != 0)],
"file.dat", sep=",", row.names=FALSE, quote=FALSE, col.names = FALSE)