使用 cat 函数写入 csv 文件

Using cat function to write on csv file

我需要使用 cat 函数向 CSV 中添加新行。请你们帮帮我好吗?我对 R 的了解有限

这是文件“name1.csv”,系统要求我将我的姓名和学生 ID 添加到前几行。

homework1 <- data.frame (homework1,Total)
homework1 <- data.frame (homework1, Commission)
# Create output file name using name1. All your output will go to this file.
sink("name1.csv")
# send the output to the csv file you just created
write.csv(homework1, file = "name1.csv")
# delete first column of the csv file
# Use cat function to write your name (First Last). This will be Row 1 of csv file. 
cat(file = "name1.csv", "Ibra", "\n", sep = ",", append=TRUE, row.names(F))

如果你想知道如何使用一个函数,你可以在它上面调用help函数:

help(cat)

Usage

cat(... , file = "", sep = " ", fill = FALSE, labels = NULL, append = FALSE)

Arguments ...

它还在继续。

默认情况下,Cat 将写入接收器。所以如果你想让你的名字在文件中首先出现。从 cating 你的名字开始:

sink("name1.csv")
cat("Ibra Lastname\n")  # sink is set, so file is not needed
write.csv(homework1)

请注意,这会使 csv 文件无效;第一行将是一个字符串,其余行将以逗号分隔值表示 homework1 的任何形状。这个作业对我来说意义不大。