为什么我的变量在R中不被识别?

为什么我的变量在R中不被识别?,r,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> <

我是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

我的第一个猜测是,从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)
以及:

dplyr::计数(tba\u hba,S1)
#一个tibble:2x2
S1 n
1     0     6
2     1     2

要克服这个问题,请检查包的加载顺序,或者更好地使用
-语法

我的第一个猜测是,从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)
以及:

dplyr::计数(tba\u hba,S1)
#一个tibble:2x2
S1 n
1     0     6
2     1     2
要克服这个问题,请检查包的加载顺序,或者更好地使用
-语法

您可能
附加(the.data.set)
然后
rm(list=ls())
分离(the.data.set)
最好不要使用
attach()
$

table(tba_hba$S1)
您可能
attach(the.data.set)
然后
rm(list=ls())
detach(the.data.set)
最好不要使用
attach()
并使用
$

table(tba_hba$S1)

我强烈地猜测你不会调用你认为你会调用的函数。尝试:

dplyr::count(df, S1)
# # A tibble: 2 x 2
#       S1     n
#    <dbl> <int>
# 1     0    11
# 2     1     4


# The error message if a S_xy is not in the data:
dplyr::count(df, S_xy)
# Error: Column `S_xy` is unknown
dplyr::count(df,S1)
##tibble:2x2
#S1 n
#     
# 1     0    11
# 2     1     4
#如果数据中没有S_xy,则显示错误消息:
dplyr::计数(df,S_xy)
#错误:列'S_xy'未知

我强烈猜测您没有调用您认为应该调用的函数。尝试:

dplyr::count(df, S1)
# # A tibble: 2 x 2
#       S1     n
#    <dbl> <int>
# 1     0    11
# 2     1     4


# The error message if a S_xy is not in the data:
dplyr::count(df, S_xy)
# Error: Column `S_xy` is unknown
dplyr::count(df,S1)
##tibble:2x2
#S1 n
#     
# 1     0    11
# 2     1     4
#如果数据中没有S_xy,则显示错误消息:
dplyr::计数(df,S_xy)
#错误:列'S_xy'未知

尝试计数(tba\u hba,tba\u hba$S1)。根据您提供的代码,您似乎尚未指定
k.file
值。因此,在阅读Excel文件之前,您需要指定:
k.file@Matt:count(tba_hba,tba_hba$S1)错误:无法使用引用的类对象使用
[/code>进行子集。这也不起作用。
\jährlich anzupassende Jahreszahl
k.jahr=2019
\dynamischer-datename
k.file=paste(“Rohdaten TBA HBA-”,k.jahr,“\u orig.xlsx”,sep=“”)
k.file
\Datei-einlesen
setwd(file.path(“P:/2\u statistikproducation/SUBMISS/01 bezonge-Daten/Kreko”,k.jahr))
TBA\u-HBA@Matt按照您的建议进行操作时,我会在read\u-fun(path=enc2native(normalizePath(normalizePath(path))中收到警告
),sheet_i=sheet,…:强制W1583/R1583C23中的文本为数字:“8590”
请使用
dput(head(data))
<1]“k.file”“tba_hba”
类“tbl_df”、“tbl”和“data.frame”:8个变量中的15个变量:
$AMT:chrTBA“TBA”TBA“TBA”…
$E0:chr“D0”D0“D0”D0“D0”…
$M:chr“T248A15”T248A15“T248A15”T248A15“…
$S1:num 1 1 0 0 0 0 0 0 0 1…
$S2:num 2 3 3 3 3 2…
$S3:num 1 1 1 1 1 1 1 1 1 1 1 1…
$S4:num 0 0 0 0 0 0 0 0 0 1…
尝试使用
。根据您提供的代码,您似乎没有为
k.file
赋值。因此,在读取Excel文件之前,您需要指定:
k.file@Matt:count(tba\u hba,tba\u hba$S1)错误:无法使用
[
使用引用类的对象。这也不起作用。
\jährlich anzupassende Jahreszahl
k.jahr=2019
\dynamischer Dateiname
k.file=paste(“Rohdaten TBA HBA-”,k.jahr,“\u orig.xlsx”,sep=”“)
k.file
k.file
\Datei-einlesen(“P:/2_statistikproducation/SUBMISS/01 Bezogene Daten/Kreko”,k.jahr))
tba_hba@Matt按照您的建议进行操作时,我会在read\u fun(path=enc2native(normalizePath(path))中得到警告
,sheet_i=sheet,…:强制W1583/R1583C23中的文本为数字:“8590”
请使用
dput(head(data))
<1]“k.file”“tba_hba”
类“tbl_df”、“tbl”和“data.frame”:8个变量中的15个变量:
$AMT:chrTBA“TBA”TBA“TBA”…
$E0:chr“D0”D0“D0”D0“D0”…
$M:chr“T248A15”T248A15“T248A15”T248A15“…
$S1:num 1 1 0 0 0 0 0 0 1…
$S2:num 2 2 3 3 3 3 2…
$S3:num 1 1 1 1 1 1 1 1 1 1…
$S4:num 0 0 0 0 0 0 0 0 1…
$Projekt:chr“Y02.9.8.1”“Y02.9.8.1”“Y34.6.1.2”“Y01.6.1.1”…
table
可以,但请永远不要使用attach!
table
可以,但请永远不要使用attach!非常好的一点!当混淆包dplyr和plyr时,可以复制原始错误消息:
plyr::count(tba_hba,S1)
。这一点很好!当混淆程序包dplyr和plyr时,可以复制原始错误消息:
plyr::count(tba_hba,S1)
。这确实是plyr和dplyr的混淆!它现在工作得非常好。我没有意识到这会有这样的效果。非常感谢。plyr和dplyr的包装采用完全不同的方法。tidyverse appraoch非常酷,但与base R非常保守的风格相比,Hadley Wickham采用了更为保守的风格amic方法和功能