as.character error

Hi Everyone,

I ran this code:

I got the error:
Error in $<*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?


What is the output of


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

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

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]))
#>     Length Class  Mode     
#> Add 21     -none- character
#>    Length     Class      Mode 
#>        21 character character
#> [1] "list"
#> 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


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.