为什么我的变量在R中无法识别?

我是R语言的初学者。我编写了一个运行良好的脚本。但是,从昨天开始,它不再识别我的变量了。我可以查看数据集,但无法执行任何分析。

tba_hba <- read_excel(k.file)
tba_hba

   AMT   E0    M     `X-Kto`    S1    S2    S3    S4 
   <chr> <chr> <chr> <chr>   <dbl> <dbl> <dbl> <dbl> 
 1 TBA   D0    T248~ X1.2.1~     1     2     1     0 
 2 TBA   D0    T248~ X1.2.1~     1     2     1     0 
 3 TBA   D0    T248~ X0.3.1~     0     3     1     0 
 4 TBA   D0    T248~ X0.3.1~     0     3     1     0 
 5 TBA   D0    T248~ X0.3.1~     0     3     1     0 
 6 TBA   D0    T248~ X0.3.1~     0     3     1     0 
 7 TBA   D0    T248~ X0.3.1~     0     3     1     0 
 8 TBA   D0    T248~ X0.3.1~     0     3     1     0 

count(tba_hba, S1)

Error in count(tba_hba, S1) : object 'S1' not found

我可以看到变量S1,但无法对其进行计数。

使用read.xlsx时出现相同的错误。

有什么想法为什么从昨天开始发生这种情况而没有更改我的数据?

编辑:这是我的数据:

structure(list(AMT = c("TBA", "TBA", "TBA", "TBA", "TBA", "TBA", 
"TBA", "TBA", "TBA", "TBA", "TBA", "TBA", "TBA", "TBA", "TBA"
), E0 = c("D0", "D0", "D0", "D0", "D0", "D0", "D0", "D0", "D0", 
"D0", "D0", "D0", "D0", "D0", "D0"), M = c("T248A15", "T248A15", 
"T248A15", "T248A15", "T248A15", "T248A15", "T248A15", "T248A15", 
"T248A15", "T248A15", "T248A15", "T248A15", "T248A15", "T248A15", 
"T248A15"), S1 = c(1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 
0), S2 = c(2, 2, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 3, 3), S3 = c(1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), S4 = c(0, 0, 0, 0, 
0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0), Projekt = c("Y02.9.8.1", "Y02.9.8.1", 
"Y34.6.1.2", "Y01.6.1.1", "Y01.6.1.1", "Y05.6.1.1", "Y04.6.1.1", 
"Y04.6.1.1", "Y05.6.1.1", "Y21.9.8.1", "Y23.9.8.1", "Y05.6.1.1", 
"Y03.6.1.1", "Y03.6.1.1", "Y05.6.1.1")), row.names = c(NA, -15L
), class = c("tbl_df", "tbl", "data.frame"))
评论
  • 你太小
    你太小 回复

    我想从Excel读取文件时发生了某些情况。如果您按照问题中的说明读取了数据,那么它应该可以工作:

    library("dplyr")
    library("readr")
    x <- (
      "AMT   E0    M     `X-Kto`    S1    S2    S3    S4
    TBA   D0    T248~ X1.2.1~     1     2     1     0
    TBA   D0    T248~ X1.2.1~     1     2     1     0
    TBA   D0    T248~ X0.3.1~     0     3     1     0
    TBA   D0    T248~ X0.3.1~     0     3     1     0
    TBA   D0    T248~ X0.3.1~     0     3     1     0
    TBA   D0    T248~ X0.3.1~     0     3     1     0
    TBA   D0    T248~ X0.3.1~     0     3     1     0
    TBA   D0    T248~ X0.3.1~     0     3     1     0")
    
    tba_hba <- read_delim(x, delim=" ", trim_ws = TRUE)
    count(tba_hba, S1)
    

    Option trim_ws is important here for the column names, but in read_excel this argument is TRUE by default.

    Suggestion: export the data to a .csv file and read it with read_csv.

  • 买依薇
    买依薇 回复

    you probably attach(the.data.set) then rm(list=ls()) or detach(the.data.set) better not to attach() and used $

    table(tba_hba$S1)