Home > OS >  Changing class from character to integer but retaining all the datas inside
Changing class from character to integer but retaining all the datas inside

Time:01-03

q7 <- dbGetQuery(conn, "SELECT TailNum AS TailNum, AVG(ontime.DepDelay) AS avg_delay, ontime.Year AS Year, planes.Year AS yearmade FROM planes JOIN ontime USING(tailnum) WHERE ontime.Cancelled = 0 AND planes.Year != '' AND planes.Year != 'None' AND ontime.Diverted = 0 AND ontime.DepDelay > 0 GROUP BY TailNum ORDER BY avg_delay")

Codes that i have tried: q7 <- data.frame( yearmade = q7.yearmade, stringsAsFactors = FALSE)

enter image description here

^ Dataframe

Hi! Basically I would like to create a new column where the Year would subtract the yearmade and be placed into a new column, but before I could do that, I found out that the data I draw from another table into this dataframe shows as character(yearmade), is there any way to change it but retain the original data inside?

CodePudding user response:

First use as.numeric() to change yearmade into a numeric variable. Then you can simply compute the difference between Year and yearmade.

I believe this will work for you.

set.seed(1)

Year <- 2000:2022
yearmade <- sample(c('2000', '1999', '1998'), length(Year), replace = TRUE)
TailNum <- sample(c('N3738B', 'N3737C', 'N37342'), length(Year), replace = TRUE)
avg_delay <- 1:length(Year)
q7 <- data.frame(TailNum, avg_delay, Year, yearmade)

# compute difference and add to data frame
q7$year_diff <- q7$Year - as.numeric(q7$yearmade)

This retains the original data, but introduces a new column year_diff.

> str(q7)
'data.frame':   23 obs. of  5 variables:
 $ TailNum  : chr  "N3738B" "N3738B" "N3737C" "N3738B" ...
 $ avg_delay: int  1 2 3 4 5 6 7 8 9 10 ...
 $ Year     : int  2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 ...
 $ yearmade : chr  "2000" "1998" "2000" "1999" ...
 $ year_diff: num  0 3 2 4 4 7 8 8 9 11 ...
  •  Tags:  
  • r dbi
  • Related