as.character error

Hi Everyone,

I ran this code:
Data$Add<-as.character(Data$Add)

I got the error:
Error in $<-.data.frame(*tmp*, Add, value = character(0)) :
replacement has 0 rows, data has 1592397

The Add column include values NA. Any suggestions on how to avoid the error?

Thanks!

What is the output of

summary(Data)

my output for Summary(Data): Add
Length: 1287005
Class: character
Mode: Character

my output for summary(Data$Add): Length Class Mode
0 NULL NULL

I have Values in column Add, Data$Add output Null, don't know why

That is not what I expected, so I am glad I asked. It seems that Data is a list, not a data frame. How is Data generated? Can you show that code that does that and then run the sequence of commands shown below? My version of Data is simply generated by the list function but you should use whatever is appropriate for your code.

Data <- list(Add = c(NA_character_ ,LETTERS[1:20]))
summary(Data)
#>     Length Class  Mode     
#> Add 21     -none- character
summary(Data$Add)
#>    Length     Class      Mode 
#>        21 character character
class(Data)
#> [1] "list"
str(Data)
#> List of 1
#>  $ Add: chr [1:21] NA "A" "B" "C" ...
Data$Add <- as.character(Data$Add)

Created on 2020-04-29 by the reprex package (v0.3.0)

Data is a dataframe, I pulled the data from a Microsoft access database. It has 54 columns, Add is one of the column, I have other columns like Add_1, Add_2.. which are similar to Add, but the error only occurred for Add, not for others.
Here is my output for Summary(Data$Add_1)
Length Class Mode
1287005 character character

Let's see if the problem can be reproduced on a subset of the data that you can then post here. Try making a smaller version of the data with just the columns Add and Add_1, that is, the "bad" column and one "good" column. The command would be

DataNew <- Data[,c("Add", "Add_1")]

then check if the error still occurs. Then trim the number of rows like this

DataNew <- DataNew[1:20,]

and again check if the error still happens. If it does, please run

dput(DataNew)

and post the results here. If the error disappears, let us know at what step that happens.

There is nothing special about the number of columns and rows I picked to keep. If posting a somewhat larger data set, produced by dput(), is a better idea, do that.

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