首页 > 解决方案 > 用 R 绘制和线性回归模型

问题描述

我正在为 R 做一个家庭作业项目,我有一个需要导入的 CSV 文件,所以对散点图和线性回归进行一些分析并创建。

有一次我的代码有一个错误,我不确定为什么或在哪里,我认为它可能在这一行:

x <- c("Name", "Washington")

CTD <- subset (Current_Data, grepl(paste(x, collapse = "|"), Current_Data$V1))

所以我是 R 的新手,这是我上过的第一堂课,我用谷歌搜索了一些,确实在这里找到了一个有一些见解的线程,但即使使用该代码,我仍然会出错。(使用 R 对人口数据进行线性回归分析

在与我班上的其他人和我的教授交谈时,我走了另一条路,虽然我不确定哪个更好。

这是我正在使用的当前代码。:

Current_Data<-read.csv("Statepop.csv",header=F,stringsAsFactors=FALSE)

Current_Data

x <- c("Name", "Washington")

CTD <- subset (Current_Data, grepl(paste(x, collapse = "|"), Current_Data$V1))

CTD

C_T_D <- t(CTD)

C_T_D

colnames(C_T_D) <- c("Year","Population")

Current_Data 是完美的(虽然那只是读取 CSV),但是当我到达 CTD 时,我认为有些东西坏了。

CTD给了我这个回应

    V1      V2      V3      V4      V5      V6      V7      V8      V9     V10
54 Washington 6742902 6821655 6892876 6962906 7052439 7163543 7294680 7425432 7535591

这里的问题是它并没有放弃这些年,所以当我用“t()”函数(C_D_T)取行并进入列时

我明白了:

C_T_D 54
V1“华盛顿”V2“6742902”
V3“6821655”
V4“6892876”
V5“6962906”
V6“7052439”
V7“7163543”
V8“7294680”
V9“7425432”
V10“7535591”

因为我已经失去了岁月,所以 Colnames 不起作用并给了我这个错误:

colnames(C_T_D) <- c("Year","Population") dimnames(x) <- dn 中的错误:“dimnames”[2] 的长度不等于数组范围

编辑:这是 Current_Data 的输出。

Current_Data V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 1 NAME 2010 2011 2012 2013 2014 2015 2016 2017 2018 2 United States 309326085 311580009 313874218 316057727 318386421 320742673 323071342 325147121 327167434 3 Northeast Region 55380645 55600532 55776729 55907823 56015864 56047587 56058789 56072676 56111079 4 Midwest Region 66974749 67152631 67336937 67564135 67752238 67869139 67996917 68156035 68308744 5 South Region 114867066 116039399 117271075 118393244 119657737 121037542 122401186 123598424 124753948 6 West Region 72103625 72787447 73489477 74192525 74960582 75788405 76614450 77319986 77993663 7 Alabama4785448 4798834 4815564 4830460 4842481 4853160 4864745 4875120 4887871 8 Alaska 713906 722038 730399 737045 736307 737547 741504 739786 737438 9 Arizona 6407774 6473497 6556629 6634999 6733840 6833596 6945452 7048876 7171646 10 Arkansas 2921978 2940407 2952109 2959549 2967726 2978407 2990410 3002997 3013825 11 California 37320903 37641823 37960782 38280824 38625139 38953142 39209127 39399349 39557045 12 Colorado 5048281 5121771 5193721 5270482 5351218 5452107 5540921 5615902 5695564 13 Connecticut 3579125 3588023 3594395 3594915 3594783 3587509 3578674 3573880 3572665 14 Delaware 899595 907316 915188 923638 932596941413 949216 957078 967171 15 District of Columbia 605085 619602 634725 650431 662513 675254 686575 695691 702455 16 Florida 18845785 19093352 19326230 19563166 19860330 20224249 20629982 20976812 21299325 17 Georgia 9711810 9801578 9901496 9973326 10069001 10181111 10304763 10413055 10519475 18 Hawaii 1363963 1379252 1394905 1408453 1414862 1422484 1428105 1424203 1420491 19 Idaho 1570773 1583828 1595441 1611530 1631479 1651523 1682930 1718904 1754208 20 Illinois 12840762 12867291 12884119 12898269 12888962 12864342 12826895 12786196 12741080 21 Indiana 6490436 6516045 6537640 6568367 6593533 6608296 6633344 6660082 6691878 22Iowa 3050767 3066054 3076097 3093078 3109504 3121460 3131785 3143637 3156145 23 Kansas 2858213 2869035 2885361 2893510 2900896 2909502 2911263 2910689 2911505 24 Kentucky 4348200 4369488 4386381 4404817 4414483 4425999 4438229 4453874 4468402 25 Louisiana 4544532 4575184 4600814 4624577 4644204 4664851 4678215 4670818 4659978 26 Maine 1327632 1328150 1327691 1328196 1330760 1328484 1331370 1335063 1338404 27 Maryland 5788642 5838991 5887072 5923704 5958165 5986717 6004692 6024891 6042718 28 Massachusetts 6566431 6613149 6663158 6713944 6763652 6795891 6826022 6863246 6902149 29 Michigan 9877535 9881521 98969309913349 9930589 9932573 9951890 9976447 9995915 30 Minnesota 5310843 5345668 5376550 5413693 5451522 5482503 5523409 5568155 5611179 31 Mississippi 2970536 2978470 2983767 2988797 2990623 2988693 2988298 2989663 2986530 32 Missouri 5995976 6009641 6024081 6040658 6056293 6071745 6087203 6108612 6126452 33 Montana 990722 997221 1003754 1013564 1021891 1030503 1040863 1053090 1062305 34 Nebraska 1829536 1840538 1853323 1865414 1879522 1891507 1905924 1917575 1929268 35 Nevada 2702464 2712799 2744566 2776972 2819012 2868666 2919772 2972​​405 3034392 36 New Hampshire 1316777 1319815 1323962 1326408 1333223 1336294 13423731349767 1356458 37 New Jersey 8799624 8827783 8845483 8858362 8866780 8870869 8874516 8888543 8908520 38 New Mexico 2064588 2080395 2087549 2092792 2090342 2090211 2092789 2093395 2095428 39 New York 19400080 19498514 19574549 19628043 19656330 19661411 19641589 19590719 19542209 40 North Carolina 9574293 9656754 9749123 9843599 9933944 10033079 10156679 10270800 10383620 41 North Dakota 674710 685136 701116 721999 737382 754022 754353 755176 760077 42 Ohio 11539327 11543463 11548369 11576576 11602973 11617850 11635003 11664129 11689442 43 Oklahoma 3759632 3787821 3818600 3853205 3878367 3909831 3926769 3932640 3943079 44 Oregon3837532 3871728 3899118 3922908 3964106 4016918 4091404 4146592 4190713 45 Pennsylvania 12711158 12744583 12766827 12776621 12789101 12785759 12783538 12790447 12807060 46 Rhode Island 1053938 1053536 1054601 1055122 1056017 1056173 1057063 1056486 1057315 47 South Carolina 4635656 4671422 4717112 4764153 4823793 4892253 4958235 5021219 5084127 48 South Dakota 816165 823484 833496 842270 849088 853933 862890 873286 882235 49 Tennessee 6355301 6397410 6451281 6493432 6540826 6590808 6645011 6708794 6770010 50 Texas 25242679 25646227 26089620 26489464 26977142 27486814 27937492 28322717 28701845 51 Utah 2775334 2814216 2853467 2897927 29373992982497 3042613 3103118 3161105 52 Vermont 625880 626979 626063 626212 625218 625197 623644 624525 626299 53 Virginia 8023680 8100469 8185229 8253053 8312076 8362907 8410946 8465207 8517685 54 Washington 6742902 6821655 6892876 6962906 7052439 7163543 7294680 7425432 7535591 55 West Virginia 1854214 1856074 1856764 1853873 1849467 1841996 1830929 1817048 1805832 56 Wisconsin 5690479 5704755 5719855 5736952 5751974 5761406 5772958 5792051 5813568 57 Wyoming 564483 567224 576270 582123 582548 585668 584290 578934 577737 58 Puerto Rico 3721525 3678732 3634488 3593077 3534874 3473166 3406495 3325001 31951532982497 3042613 3103118 3161105 52 Vermont 625880 626979 626063 626212 625218 625197 623644 624525 626299 53 Virginia 8023680 8100469 8185229 8253053 8312076 8362907 8410946 8465207 8517685 54 Washington 6742902 6821655 6892876 6962906 7052439 7163543 7294680 7425432 7535591 55 West Virginia 1854214 1856074 1856764 1853873 1849467 1841996 1830929 1817048 1805832 56 Wisconsin 5690479 5704755 5719855 5736952 5751974 5761406 5772958 5792051 5813568 57 Wyoming 564483 567224 576270 582123 582548 585668 584290 578934 577737 58 Puerto Rico 3721525 3678732 3634488 3593077 3534874 3473166 3406495 3325001 31951532982497 3042613 3103118 3161105 52 Vermont 625880 626979 626063 626212 625218 625197 623644 624525 626299 53 Virginia 8023680 8100469 8185229 8253053 8312076 8362907 8410946 8465207 8517685 54 Washington 6742902 6821655 6892876 6962906 7052439 7163543 7294680 7425432 7535591 55 West Virginia 1854214 1856074 1856764 1853873 1849467 1841996 1830929 1817048 1805832 56 Wisconsin 5690479 5704755 5719855 5736952 5751974 5761406 5772958 5792051 5813568 57 Wyoming 564483 567224 576270 582123 582548 585668 584290 578934 577737 58 Puerto Rico 3721525 3678732 3634488 3593077 3534874 3473166 3406495 3325001 31951533103118 3161105 52 Vermont 625880 626979 626063 626212 625218 625197 623644 624525 626299 53 Virginia 8023680 8100469 8185229 8253053 8312076 8362907 8410946 8465207 8517685 54 Washington 6742902 6821655 6892876 6962906 7052439 7163543 7294680 7425432 7535591 55 West Virginia 1854214 1856074 1856764 1853873 1849467 1841996 1830929 1817048 1805832 56 Wisconsin 5690479 5704755 5719855 5736952 5751974 5761406 5772958 5792051 5813568 57 Wyoming 564483 567224 576270 582123 582548 585668 584290 578934 577737 58 Puerto Rico 3721525 3678732 3634488 3593077 3534874 3473166 3406495 3325001 31951533103118 3161105 52 Vermont 625880 626979 626063 626212 625218 625197 623644 624525 626299 53 Virginia 8023680 8100469 8185229 8253053 8312076 8362907 8410946 8465207 8517685 54 Washington 6742902 6821655 6892876 6962906 7052439 7163543 7294680 7425432 7535591 55 West Virginia 1854214 1856074 1856764 1853873 1849467 1841996 1830929 1817048 1805832 56 Wisconsin 5690479 5704755 5719855 5736952 5751974 5761406 5772958 5792051 5813568 57 Wyoming 564483 567224 576270 582123 582548 585668 584290 578934 577737 58 Puerto Rico 3721525 3678732 3634488 3593077 3534874 3473166 3406495 3325001 3195153Vermont 625880 626979 626063 626212 625218 625197 623644 624525 626299 53 Virginia 8023680 8100469 8185229 8253053 8312076 8362907 8410946 8465207 8517685 54 Washington 6742902 6821655 6892876 6962906 7052439 7163543 7294680 7425432 7535591 55 West Virginia 1854214 1856074 1856764 1853873 1849467 1841996 1830929 1817048 1805832 56 Wisconsin 5690479 5704755 5719855 5736952 5751974 5761406 5772958 5792051 5813568 57 Wyoming 564483 567224 576270 582123 582548 585668 584290 578934 577737 58 Puerto Rico 3721525 3678732 3634488 3593077 3534874 3473166 3406495 3325001 3195153Vermont 625880 626979 626063 626212 625218 625197 623644 624525 626299 53 Virginia 8023680 8100469 8185229 8253053 8312076 8362907 8410946 8465207 8517685 54 Washington 6742902 6821655 6892876 6962906 7052439 7163543 7294680 7425432 7535591 55 West Virginia 1854214 1856074 1856764 1853873 1849467 1841996 1830929 1817048 1805832 56 Wisconsin 5690479 5704755 5719855 5736952 5751974 5761406 5772958 5792051 5813568 57 Wyoming 564483 567224 576270 582123 582548 585668 584290 578934 577737 58 Puerto Rico 3721525 3678732 3634488 3593077 3534874 3473166 3406495 3325001 3195153625218 625197 623644 624525 626299 53 Virginia 8023680 8100469 8185229 8253053 8312076 8362907 8410946 8465207 8517685 54 Washington 6742902 6821655 6892876 6962906 7052439 7163543 7294680 7425432 7535591 55 West Virginia 1854214 1856074 1856764 1853873 1849467 1841996 1830929 1817048 1805832 56 Wisconsin 5690479 5704755 5719855 5736952 5751974 5761406 5772958 5792051 5813568 57 Wyoming 564483 567224 576270 582123 582548 585668 584290 578934 577737 58 Puerto Rico 3721525 3678732 3634488 3593077 3534874 3473166 3406495 3325001 3195153625218 625197 623644 624525 626299 53 Virginia 8023680 8100469 8185229 8253053 8312076 8362907 8410946 8465207 8517685 54 Washington 6742902 6821655 6892876 6962906 7052439 7163543 7294680 7425432 7535591 55 West Virginia 1854214 1856074 1856764 1853873 1849467 1841996 1830929 1817048 1805832 56 Wisconsin 5690479 5704755 5719855 5736952 5751974 5761406 5772958 5792051 5813568 57 Wyoming 564483 567224 576270 582123 582548 585668 584290 578934 577737 58 Puerto Rico 3721525 3678732 3634488 3593077 3534874 3473166 3406495 3325001 31951538312076 8362907 8410946 8465207 8517685 54 Washington 6742902 6821655 6892876 6962906 7052439 7163543 7294680 7425432 7535591 55 West Virginia 1854214 1856074 1856764 1853873 1849467 1841996 1830929 1817048 1805832 56 Wisconsin 5690479 5704755 5719855 5736952 5751974 5761406 5772958 5792051 5813568 57 Wyoming 564483 567224 576270 582123 582548 585668 584290 578934 577737 58 波多黎各 3721525 3678732 3634488 3593077 3534874 3473166 3406495 3325001 31951538312076 8362907 8410946 8465207 8517685 54 Washington 6742902 6821655 6892876 6962906 7052439 7163543 7294680 7425432 7535591 55 West Virginia 1854214 1856074 1856764 1853873 1849467 1841996 1830929 1817048 1805832 56 Wisconsin 5690479 5704755 5719855 5736952 5751974 5761406 5772958 5792051 5813568 57 Wyoming 564483 567224 576270 582123 582548 585668 584290 578934 577737 58 波多黎各 3721525 3678732 3634488 3593077 3534874 3473166 3406495 3325001 31951531849467 1841996 1830929 1817048 1805832 56 Wisconsin 5690479 5704755 5719855 5736952 5751974 5761406 5772958 5792051 5813568 57 Wyoming 564483 567224 576270 582123 582548 585668 584290 578934 577737 58 Puerto Rico 3721525 3678732 3634488 3593077 3534874 3473166 3406495 3325001 31951531849467 1841996 1830929 1817048 1805832 56 Wisconsin 5690479 5704755 5719855 5736952 5751974 5761406 5772958 5792051 5813568 57 Wyoming 564483 567224 576270 582123 582548 585668 584290 578934 577737 58 Puerto Rico 3721525 3678732 3634488 3593077 3534874 3473166 3406495 3325001 3195153

标签: rplotlm

解决方案


t() 函数正在创建一个维度大于 2 的矩阵。错误是说您只给矩阵两个列名,但矩阵数组需要两个以上。

你这样做是有原因的header = F吗?如果没有,那么以下可能会起作用:

Current_Data <- read.csv("nst-est2018-alldata.csv", header=T,stringsAsFactors=FALSE)

head(Current_Data)

x <- c("Name", "Washington")

CTD <- subset(Current_Data, grepl(paste(x, collapse = "|"), Current_Data$NAME))

CTD

# the data are in wide format, but you seem to want them in long format
C_T_D <- stack(CTD)[-1,]

C_T_D

# looks like our columns are switched
C_T_D2 <- C_T_D[,c(2,1)]

colnames(C_T_D2) <- c("Year","Population")

#to make the data easier to work with
C_T_D2$Year <- as.numeric(str_extract_all(C_T_D2$Year, "[0-9]+"))
C_T_D2$Population <- as.numeric(C_T_D2$Population)

推荐阅读