1

I'm quite a beginner in R. I wrote a script that worked just fine. However, since yesterday, it just doesn't recognize my variables anymore. I can view the dataset, but can't do any analysis.

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

I can see the variable S1, but can't do a count on it.

I get the same error when using read.xlsx.

Any ideas why this happens since yesterday without changing anything on my data?

EDIT: This are my data:

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"))
6
  • Try count(tba_hba, tba_hba$S1). Based on the code you provided, it looks like you haven't assigned k.file a value. So before you read in your Excel file, you need to specify: k.file <- "C:/filepath/file.xlsx" Commented Apr 24, 2020 at 16:51
  • @Matt: count(tba_hba, tba_hba$S1) Error: Can't subset with [ using an object of class quoted. This also doen't work. #jährlich anzupassende Jahreszahl k.jahr = 2019 #dynamischer Dateiname k.file = paste("Rohdaten-TBA-HBA-",k.jahr,"_orig.xlsx", sep="") k.file #Datei einlesen setwd(file.path("P:/2_Statistikproduktion/SUBMISS/01 Bezogene Daten/Kreko", k.jahr)) tba_hba <- read_excel(k.file) tba_hba Commented Apr 24, 2020 at 16:59
  • @Matt When doing it like you suggest, I then get the warning In read_fun(path = enc2native(normalizePath(path)), sheet_i = sheet, ... : Coercing text to numeric in W1583 / R1583C23: '8590' Commented Apr 24, 2020 at 17:03
  • 1
    Please provide your actual data with dput(head(data)). Commented Apr 24, 2020 at 17:06
  • > ls() [1] "k.file" "tba_hba" > str(tba_hba) Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 15 obs. of 8 variables: $ AMT : chr "TBA" "TBA" "TBA" "TBA" … $ E0 : chr "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 2 3 3 3 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 0 1 … $ Projekt: chr "Y02.9.8.1" "Y02.9.8.1" "Y34.6.1.2" "Y01.6.1.1" … Commented Apr 24, 2020 at 17:13

3 Answers 3

2

I strongly guess you do not call the function you think you would. Try:

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
Sign up to request clarification or add additional context in comments.

1 Comment

Very good point! The original error message can be reproduced when confusing package dplyr and plyr: plyr::count(tba_hba, S1).
1

My first guess was that something happened while reading the file from Excel. If one reads the data as written in your question, it should work:

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.

Then @mnist came with the suggestion, that another function was used. Confusing plyr with dplyr is indeed a reasonable explanation.

Compare the following:

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

and:

dplyr::count(tba_hba, S1)

# A tibble: 2 x 2
     S1     n
  <dbl> <int>
1     0     6
2     1     2

To overcome this, check the order how the packages are loaded or, even better, use the ::-syntax.

2 Comments

It was indeed the confusion of plyr and dplyr! It works now perfectly fine. I did not realize that this can have such an effect. Thank you very much.
Packages plyr and dplyr follow completely different approaches. The tidyverse-appraoch is very cool, but in contrast to the very conservative style of base R, Hadley Wickham follows a more dynamic approach and functionality changes. If you load both, plyr should come before dplyr, otherwise a warning is shown.
0

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

table(tba_hba$S1)

1 Comment

tableis ok, but please never use attach!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.