2

I am having trouble with data frames in R. I create the data frame with defined datatypes but when I append data it changes the data type. I don't understand why this is happening or the find a solution. Can anyone help?

df<- data.frame(year=integer(), yes_no=character())
df[nrow(df) + 1,] = c(2022 ,"Y") 
Adrian T
  • 21
  • 3

3 Answers3

4

Short answer:

Vectors are sometimes also referred to as the ‘nuts & bolts’ in R as they build the basis for all complex objects such as data frames or fitted regression models:

  • You "pass" an atomic vector c(2022, "Y") to row 1 of df. In R atomic vectors contain only elements of same type e.g. all numeric, or all character...etc...
  • Atomic vectors are different to lists, lists can contain different types.
  • Atomic vectors are constructed with the c() function or the vector function.

In your case: character is over integer in the hierarchy of atomic vectors: therefore 2022 is transformed to character type.

Solution:

df$year <- as.integer(df$year)
str(df)
'data.frame':   1 obs. of  2 variables:
 $ year  : int 2022
 $ yes_no: chr "Y"
TarJae
  • 72,363
  • 6
  • 19
  • 66
0

can you try to do something like :

df<- data.frame(year=numeric(), yes_no=character())
df <- rbind(df, list(2022 ,"Y"))
Florian M.
  • 19
  • 3
0

In fact, the code is correct and doing what is expected from a data.frame type. I suggest to use a conversion like @TarJae mentioned or switch to tibbles as I describe below:

> df <- data.frame(year=integer(), yes_no=character())
> 
> class(df)
[1] "data.frame"
> class(df$year)
[1] "integer"
> class(df$yes_no)
[1] "factor"
> 
> df <- tibble(year=integer(), yes_no=character())
> 
> class(df)
[1] "tbl_df"     "tbl"        "data.frame"
> class(df$year)
[1] "integer"
> class(df$yes_no)
[1] "character"
>

I hope this is helpful !!

Kind regards, Augusto.

AugtPelle
  • 549
  • 1
  • 10