如何将列名分配给 table
How to assign column names to a table
我正在 Rstudio 中使用以下 data。我正在尝试将列名分配给数据。我使用了以下命令:
nlsdata -> read.table("C:/Users/perdue/Desktop/Adv.MicroEconometrics/HA 3/data/nls.dat",
header = FALSE, dec = ".")
此命令 returns 作为第一行列名称 'v1 v2 v3... v52'。当我跟着
colnames(nlsdata)
我得到了一份名单:v1, v2, ..., v52
。那么
col.names(nlsdata) <- c("inputid","nearc2","nearc4","nearc4a","nearc4b","ed76","ed66","age76",
"daded","nodaded","momed","nomomed","weight","momdad14","sinmom14",
"step14","reg661","reg662","reg663","reg664","reg665","reg666","reg667",
"reg668","reg669","south66","work76","work78","lwage76","lwage78",
"famed","black","smsa76r","smsa78r","reg76r","reg78r","reg80r",
"smsa66r","wage76","wage78","wage80","noint78","enroll76","enroll78",
"enroll80","kww","iq","marsta76","marsta78","marsta80","libcrd14")
where newname[i] is the ith column name of dataframe nlsdata`
Error: unexpected symbol in ""south66","work76","work78","lwage76","lwage78","famed","black","smsa76r","smsa78r","reg76r","reg78r","reg80r","smsa66r","wage76","wage78","wage80", "noint78","enroll76","enroll78","enroll80","
错误消息似乎表明存在语法错误。我已经看过——不止一次——但不能 find/recognize 一个。
正确的代码是
nlsdata<-read.table("C:/Users/name/Desktop/nls.dat", header = FALSE, skip = 1, dec = ".")
然后用 colnames
添加列名
colnames(nlsdata)<-c("inputid","nearc2","nearc4","nearc4a","nearc4b","ed76","ed66","age76","daded","nodaded","momed","nomomed","weight","momdad14","sinmom14","step14","reg661","reg662","reg663","reg664","reg665","reg666","reg667","reg668","reg669","south66","work76","work78","lwage76","lwage78","famed","black","smsa76r","smsa78r","reg76r","reg78r","reg80r","smsa66r","wage76","wage78","wage80","noint78","enroll76","enroll78","enroll80","kww","iq","marsta76","marsta78","marsta80","libcrd14")
并检查
head(nlsdata)
您遇到的错误似乎与以下两个引号有关:
""south66"
readr::read_table()
很好地读取文件:
library(readr)
url <- "https://raw.githubusercontent.com/108michael/ms_thesis/ca258bc684c3a6f8ade13769590439ad1e8387d7/nls.dat"
col_names = c("inputid","nearc2","nearc4","nearc4a","nearc4b","ed76","ed66",
"age76","daded","nodaded","momed","nomomed","weight","momdad14","sinmom14",
"step14","reg661", "reg662","reg663","reg664","reg665","reg666","reg667",
"reg668","reg669","south66","work76","work78","lwage76","lwage78","famed",
"black","smsa76r","smsa78r","reg76r","reg78r","reg80r","smsa66r","wage76",
"wage78","wage80","noint78","enroll76","enroll78","enroll80",
"kww","iq","marsta76","marsta78","marsta80","libcrd14")
read_table( url, col_names = col_names, na = "." )
#> Parsed with column specification:
#> cols(
#> .default = col_integer(),
#> daded = col_double(),
#> momed = col_double(),
#> lwage76 = col_double(),
#> lwage78 = col_double()
#> )
#> See spec(...) for full column specifications.
#> # A tibble: 3,613 x 51
#> inputid nearc2 nearc4 nearc4a nearc4b ed76 ed66 age76 daded nodaded
#> <int> <int> <int> <int> <int> <int> <int> <int> <dbl> <int>
#> 1 2 0 0 0 0 7 5 29 9.94 1
#> 2 3 0 0 0 0 12 11 27 8.00 0
#> 3 4 0 0 0 0 12 12 34 14.00 0
#> 4 5 1 1 1 0 11 11 27 11.00 0
#> 5 6 1 1 1 0 12 12 34 8.00 0
#> 6 7 1 1 1 0 12 11 26 9.00 0
#> 7 8 1 1 1 0 18 16 33 14.00 0
#> 8 9 1 1 1 0 14 13 29 14.00 0
#> 9 10 1 1 1 0 12 12 28 12.00 0
#> 10 11 1 1 1 0 12 12 29 12.00 0
#> # ... with 3,603 more rows, and 41 more variables: momed <dbl>,
#> # nomomed <int>, weight <int>, momdad14 <int>, sinmom14 <int>,
#> # step14 <int>, reg661 <int>, reg662 <int>, reg663 <int>, reg664 <int>,
#> # reg665 <int>, reg666 <int>, reg667 <int>, reg668 <int>, reg669 <int>,
#> # south66 <int>, work76 <int>, work78 <int>, lwage76 <dbl>,
#> # lwage78 <dbl>, famed <int>, black <int>, smsa76r <int>, smsa78r <int>,
#> # reg76r <int>, reg78r <int>, reg80r <int>, smsa66r <int>, wage76 <int>,
#> # wage78 <int>, wage80 <int>, noint78 <int>, enroll76 <int>,
#> # enroll78 <int>, enroll80 <int>, kww <int>, iq <int>, marsta76 <int>,
#> # marsta78 <int>, marsta80 <int>, libcrd14 <int>
我正在 Rstudio 中使用以下 data。我正在尝试将列名分配给数据。我使用了以下命令:
nlsdata -> read.table("C:/Users/perdue/Desktop/Adv.MicroEconometrics/HA 3/data/nls.dat",
header = FALSE, dec = ".")
此命令 returns 作为第一行列名称 'v1 v2 v3... v52'。当我跟着
colnames(nlsdata)
我得到了一份名单:v1, v2, ..., v52
。那么
col.names(nlsdata) <- c("inputid","nearc2","nearc4","nearc4a","nearc4b","ed76","ed66","age76",
"daded","nodaded","momed","nomomed","weight","momdad14","sinmom14",
"step14","reg661","reg662","reg663","reg664","reg665","reg666","reg667",
"reg668","reg669","south66","work76","work78","lwage76","lwage78",
"famed","black","smsa76r","smsa78r","reg76r","reg78r","reg80r",
"smsa66r","wage76","wage78","wage80","noint78","enroll76","enroll78",
"enroll80","kww","iq","marsta76","marsta78","marsta80","libcrd14")
where newname[i] is the ith column name of dataframe nlsdata`
Error: unexpected symbol in ""south66","work76","work78","lwage76","lwage78","famed","black","smsa76r","smsa78r","reg76r","reg78r","reg80r","smsa66r","wage76","wage78","wage80", "noint78","enroll76","enroll78","enroll80","
错误消息似乎表明存在语法错误。我已经看过——不止一次——但不能 find/recognize 一个。
正确的代码是
nlsdata<-read.table("C:/Users/name/Desktop/nls.dat", header = FALSE, skip = 1, dec = ".")
然后用 colnames
添加列名colnames(nlsdata)<-c("inputid","nearc2","nearc4","nearc4a","nearc4b","ed76","ed66","age76","daded","nodaded","momed","nomomed","weight","momdad14","sinmom14","step14","reg661","reg662","reg663","reg664","reg665","reg666","reg667","reg668","reg669","south66","work76","work78","lwage76","lwage78","famed","black","smsa76r","smsa78r","reg76r","reg78r","reg80r","smsa66r","wage76","wage78","wage80","noint78","enroll76","enroll78","enroll80","kww","iq","marsta76","marsta78","marsta80","libcrd14")
并检查
head(nlsdata)
您遇到的错误似乎与以下两个引号有关:
""south66"
readr::read_table()
很好地读取文件:
library(readr)
url <- "https://raw.githubusercontent.com/108michael/ms_thesis/ca258bc684c3a6f8ade13769590439ad1e8387d7/nls.dat"
col_names = c("inputid","nearc2","nearc4","nearc4a","nearc4b","ed76","ed66",
"age76","daded","nodaded","momed","nomomed","weight","momdad14","sinmom14",
"step14","reg661", "reg662","reg663","reg664","reg665","reg666","reg667",
"reg668","reg669","south66","work76","work78","lwage76","lwage78","famed",
"black","smsa76r","smsa78r","reg76r","reg78r","reg80r","smsa66r","wage76",
"wage78","wage80","noint78","enroll76","enroll78","enroll80",
"kww","iq","marsta76","marsta78","marsta80","libcrd14")
read_table( url, col_names = col_names, na = "." )
#> Parsed with column specification:
#> cols(
#> .default = col_integer(),
#> daded = col_double(),
#> momed = col_double(),
#> lwage76 = col_double(),
#> lwage78 = col_double()
#> )
#> See spec(...) for full column specifications.
#> # A tibble: 3,613 x 51
#> inputid nearc2 nearc4 nearc4a nearc4b ed76 ed66 age76 daded nodaded
#> <int> <int> <int> <int> <int> <int> <int> <int> <dbl> <int>
#> 1 2 0 0 0 0 7 5 29 9.94 1
#> 2 3 0 0 0 0 12 11 27 8.00 0
#> 3 4 0 0 0 0 12 12 34 14.00 0
#> 4 5 1 1 1 0 11 11 27 11.00 0
#> 5 6 1 1 1 0 12 12 34 8.00 0
#> 6 7 1 1 1 0 12 11 26 9.00 0
#> 7 8 1 1 1 0 18 16 33 14.00 0
#> 8 9 1 1 1 0 14 13 29 14.00 0
#> 9 10 1 1 1 0 12 12 28 12.00 0
#> 10 11 1 1 1 0 12 12 29 12.00 0
#> # ... with 3,603 more rows, and 41 more variables: momed <dbl>,
#> # nomomed <int>, weight <int>, momdad14 <int>, sinmom14 <int>,
#> # step14 <int>, reg661 <int>, reg662 <int>, reg663 <int>, reg664 <int>,
#> # reg665 <int>, reg666 <int>, reg667 <int>, reg668 <int>, reg669 <int>,
#> # south66 <int>, work76 <int>, work78 <int>, lwage76 <dbl>,
#> # lwage78 <dbl>, famed <int>, black <int>, smsa76r <int>, smsa78r <int>,
#> # reg76r <int>, reg78r <int>, reg80r <int>, smsa66r <int>, wage76 <int>,
#> # wage78 <int>, wage80 <int>, noint78 <int>, enroll76 <int>,
#> # enroll78 <int>, enroll80 <int>, kww <int>, iq <int>, marsta76 <int>,
#> # marsta78 <int>, marsta80 <int>, libcrd14 <int>