如何在没有索引的情况下在 R 中写入 CSV ?
我们知道,当我们将一些数据从 DataFrame 写入 CSV 文件时,会自动创建一列用于索引。我们可以通过一些修改将其删除。因此,在本文中,我们将了解如何在没有索引的情况下在 R 中编写 CSV。
使用 write.csv() 写入 csv 文件。
句法:
write.csv(data,path)
让我们先看看当数据写入 CSV 时索引是如何出现的。
例子:
R
Country <- c("China", "India", "United States", "Indonesia", "Pakistan")
Population_1_july_2018 <- c("1,427,647,786", "1,352,642,280",
"327,096,265", "267,670,543", "212,228,286")
Population_1_july_2019 <- c("1,433,783,686", "1,366,417,754",
"329,064,917", "270,625,568", "216,565,318")
change_in_percents <- c("+0.43%", "+1.02%", "+0.60%", "+1.10%", "+2.04%")
data <- data.frame(Country, Population_1_july_2018, Population_1_july_2019, change_in_percents)
print(data)
write.csv(data,"C:\\Users\\...YOUR PATH...\\population.csv")
print ('CSV file written Successfully :)')
R
Country <- c("China", "India", "United States", "Indonesia", "Pakistan")
Population_1_july_2018 <- c("1,427,647,786", "1,352,642,280",
"327,096,265", "267,670,543", "212,228,286")
Population_1_july_2019 <- c("1,433,783,686", "1,366,417,754",
"329,064,917", "270,625,568", "216,565,318")
change_in_percents <- c("+0.43%", "+1.02%", "+0.60%", "+1.10%", "+2.04%")
data <- data.frame(Country, Population_1_july_2018, Population_1_july_2019, change_in_percents)
write.csv(data,"C:\\Users\\..YOUR PATH...\\population.csv", row.names = FALSE)
print ('CSV file written Successfully :)')
输出:
现在让我们看看如何删除这些索引,因为在使用 write.csv()函数将数据写入 csv 文件时,只需将 row.names 参数设置为 False。默认情况下,它将为 TRUE 并为 CSV 文件创建一个额外的列作为索引列。
例子:
电阻
Country <- c("China", "India", "United States", "Indonesia", "Pakistan")
Population_1_july_2018 <- c("1,427,647,786", "1,352,642,280",
"327,096,265", "267,670,543", "212,228,286")
Population_1_july_2019 <- c("1,433,783,686", "1,366,417,754",
"329,064,917", "270,625,568", "216,565,318")
change_in_percents <- c("+0.43%", "+1.02%", "+0.60%", "+1.10%", "+2.04%")
data <- data.frame(Country, Population_1_july_2018, Population_1_july_2019, change_in_percents)
write.csv(data,"C:\\Users\\..YOUR PATH...\\population.csv", row.names = FALSE)
print ('CSV file written Successfully :)')
输出: