[英]Writing a list into a .dat in R using write.table
What I want:我想要的是:
1804 1804
1808 1808
1812 1812
etc... 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
.在 output 中获得“x”的原因是因为这是使用
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.如果您不想在 output 中包含列名,请使用
col.names = FALSE
,它将仅打印 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.