简体   繁体   English

错误:在 R 中找不到 object。 标题未命名来自.csv 文件

[英]Error: object not found in R. Headers not naming from .csv file

I am new to R and I keep getting inconsistent results with trying to display a column of data from a csv.我是 R 的新手,尝试显示来自 csv 的一列数据时,我不断得到不一致的结果。 I am able to import the csv into R without issue, but I can't call out the individual columns.我可以毫无问题地将 csv 导入到 R 中,但我无法调用各个列。

Here's my code:这是我的代码:

setwd('mypath')
cdata <- read.csv(file="cendata.csv",header=TRUE, sep=",")
cdata

This prints out the following:这将打印出以下内容:

   year       pop
1  2010 2,775,332
2  2011 2,814,384
3  2012 2,853,375
4  2013 2,897,640
5  2014 2,936,879
6  2015 2,981,835
7  2016 3,041,868
8  2017 3,101,042
9  2018 3,153,550
10 2019 3,205,958

When I try to plot the following, the columns cannot be found.当我尝试 plot 以下时,找不到列。

plot(pop,year)

Error: object 'pop' not found错误:未找到 object 'pop'

I even checked if the column names existed, and only data shows up.我什至检查了列名是否存在,只显示数据。

ls()
[1] "data" 

I can manually enter the data and label them "pop" and "year" but that kind of defeats the point of importing the csv.我可以手动输入数据和 label 它们“流行”和“年份”,但这违背了导入 csv 的意义。

Is there a way to label each header as an object?有没有办法将 label 每个 header 作为 object?

year and pop are not independent objects. yearpop不是独立的对象。 You need to refer them as part of the dataframe you have imported.您需要将它们作为您导入的 dataframe 的一部分引用。 Also you might need to remove "," from the numbers to turn them to numeric before plotting.此外,您可能需要从数字中删除","以在绘图之前将它们转换为数字。 Try:尝试:

cdata$pop <- as.numeric(gsub(',', '', cdata$pop))
plot(cdata$year, cdata$pop)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM