Creating factors from characters in csv file and Rstuio(in reprex)

Here is the file in reprex. The thing is, I seemed to connect to the csv file if I'm not mistaken, in normal coding efforts, and successfully created a str(BankCustomer) profile.
When I tried to convert to factors, the program seemed to register the BankCustomer1 profile, but nothign changed in the data structure. Here is the code in reprex, and the results from which it was taken in regular code. I do that, because ti looks like the results are different.
First the reprex:

BankCustomer  <- read.csv("Demo 1_ Identifying Data Structures.csv")
#> Warning in file(file, "rt"): cannot open file 'Demo 1_ Identifying Data
#> Structures.csv': No such file or directory
#> Error in file(file, "rt"): cannot open the connection
getwd()
#> [1] "C:/Users/Administrator/AppData/Local/Temp/RtmpqYfUnp/reprex-22808bf25f4-bad-grub"
View(BankCustomer)
#> Error in as.data.frame(x): object 'BankCustomer' not found
str(BankCustomer)
#> Error in str(BankCustomer): object 'BankCustomer' not found
BankCustomer1 <- read.csv("Demo 1_ Identifying Data Structures.csv",stringsAsFactors=FALSE)
#> Warning in file(file, "rt"): cannot open file 'Demo 1_ Identifying Data
#> Structures.csv': No such file or directory
#> Error in file(file, "rt"): cannot open the connection
str(BankCustomer1)
#> Error in str(BankCustomer1): object 'BankCustomer1' not found

as.factor(BankCustomer1)
#> Error in is.factor(x): object 'BankCustomer1' not found
str(BankCustomer1)
#> Error in str(BankCustomer1): object 'BankCustomer1' not found

Created on 2021-08-05 by the reprex package (v2.0.0)

Now the file as I originally wrote it:

BankCustomer  <- read.csv("Demo 1_ Identifying Data Structures.csv")
getwd()
View(BankCustomer)
str(BankCustomer)
BankCustomer1 <- read.csv("Demo 1_ Identifying Data Structures.csv",stringsAsFactors=FALSE)
str(BankCustomer1)
 
as.factor(BankCustomer1)
str(BankCustomer1)

and the output of the (two) str statements are the same...

 str(BankCustomer)
'data.frame':	4521 obs. of  15 variables:
 $ ï..age   : int  30 33 35 30 59 35 36 39 41 43 ...
 $ job      : chr  "unemployed" "services" "management" "management" ...
 $ marital  : chr  "married" "married" "single" "married" ...
 $ education: chr  "primary" "secondary" "tertiary" "tertiary" ...
 $ default  : chr  "no" "no" "no" "no" ...
 $ housing  : chr  "no" "yes" "yes" "yes" ...
 $ loan     : chr  "no" "yes" "no" "yes" ...
 $ contact  : chr  "cellular" "cellular" "cellular" "unknown" ...
 $ month    : chr  "oct" "may" "apr" "jun" ...
 $ day      : int  19 11 16 3 5 23 14 6 14 17 ...
 $ duration : int  79 220 185 199 226 141 341 151 57 313 ...
 $ campaign : int  1 1 1 4 1 2 1 2 2 1 ...
 $ pdays    : int  -1 339 330 -1 -1 176 330 -1 -1 147 ...
 $ previous : int  0 4 1 0 0 3 2 0 0 2 ...
 $ poutcome : chr  "unknown" "failure" "failure" "unknown" ...
> BankCustomer1 <- read.csv("Demo 1_ Identifying Data Structures.csv",stringsAsFactors=FALSE)
> str(BankCustomer1)
'data.frame':	4521 obs. of  15 variables:
 $ ï..age   : int  30 33 35 30 59 35 36 39 41 43 ...
 $ job      : chr  "unemployed" "services" "management" "management" ...
 $ marital  : chr  "married" "married" "single" "married" ...
 $ education: chr  "primary" "secondary" "tertiary" "tertiary" ...
 $ default  : chr  "no" "no" "no" "no" ...
 $ housing  : chr  "no" "yes" "yes" "yes" ...
 $ loan     : chr  "no" "yes" "no" "yes" ...
 $ contact  : chr  "cellular" "cellular" "cellular" "unknown" ...
 $ month    : chr  "oct" "may" "apr" "jun" ...
 $ day      : int  19 11 16 3 5 23 14 6 14 17 ...
 $ duration : int  79 220 185 199 226 141 341 151 57 313 ...
 $ campaign : int  1 1 1 4 1 2 1 2 2 1 ...
 $ pdays    : int  -1 339 330 -1 -1 176 330 -1 -1 147 ...
 $ previous : int  0 4 1 0 0 3 2 0 0 2 ...
 $ poutcome : chr  "unknown" "failure" "failure" "unknown" ...
>  
> as.factor(Bomer1)
Error in is.factor(x) : object 'Bomer1' not found
> str(BankCustomer1)
'data.frame':	4521 obs. of  15 variables:
 $ ï..age   : int  30 33 35 30 59 35 36 39 41 43 ...
 $ job      : chr  "unemployed" "services" "management" "management" ...
 $ marital  : chr  "married" "married" "single" "married" ...
 $ education: chr  "primary" "secondary" "tertiary" "tertiary" ...
 $ default  : chr  "no" "no" "no" "no" ...
 $ housing  : chr  "no" "yes" "yes" "yes" ...
 $ loan     : chr  "no" "yes" "no" "yes" ...
 $ contact  : chr  "cellular" "cellular" "cellular" "unknown" ...
 $ month    : chr  "oct" "may" "apr" "jun" ...
 $ day      : int  19 11 16 3 5 23 14 6 14 17 ...
 $ duration : int  79 220 185 199 226 141 341 151 57 313 ...
 $ campaign : int  1 1 1 4 1 2 1 2 2 1 ...
 $ pdays    : int  -1 339 330 -1 -1 176 330 -1 -1 147 ...
 $ previous : int  0 4 1 0 0 3 2 0 0 2 ...
 $ poutcome : chr  "unknown" "failure" "failure" "unknown" ...
> as.factor(BankCustomer1)

In latest R versions stringsAsFactors defaults to FALSE, that is why you get the same result.

It is not clear what you are trying to do but you can't cover an entire data frame object to factor that way

That function is meant to be used with individual variables, for example

BankCustomer1$job <- as.factor(BankCustomer1$job)

Also, it seems you are very confused about what a reproducible example (reprex) actually is, please read the guide in the link below and try to make your question providing a proper reprex.

Thanks for the clarification! I appreciate your efforts on my behalf!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.