【问题标题】:How to assign column names to a table如何将列名分配给表
【发布时间】:2018-04-29 17:27:16
【问题描述】:

我正在 Rstudio 中使用以下 data。我正在尝试为数据分配列名。我使用了以下命令:

nlsdata -> read.table("C:/Users/perdue/Desktop/Adv.MicroEconometrics/HA 3/data/nls.dat", 
                       header = FALSE, dec = ".")

此命令返回第一行列名“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","

错误消息似乎表明存在语法错误。我看过——不止一次——但找不到/认不出一个。

【问题讨论】:

  • 您确定列数正确吗?你能让giving us a demo 重现这个问题吗?
  • 您的 read.table 命令已经出现问题,因为您将所有变量都限制在一列中,因为数据导入无法按您的预期工作。
  • @alex2006 感谢您的评论。你知道我该如何补救吗?
  • 尝试删除sep=",",因为您的数据没有用逗号分隔。

标签: r names


【解决方案1】:

正确的代码是

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)

【讨论】:

  • 感谢您的回答!它与一个小编辑一起工作:skip = 1 导致我丢失了我的第一行数据。
【解决方案2】:

你得到的错误似乎是关于这两个引号:

""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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-06
    • 1970-01-01
    • 2010-09-21
    • 1970-01-01
    • 1970-01-01
    • 2020-03-06
    • 2018-05-18
    • 1970-01-01
    相关资源
    最近更新 更多