Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
311 views
in Technique[技术] by (71.8m points)

r - Changing Class and Mode from Character to Numeric

Below is a sample dataset and a few lines of code that are troubling me. I can not figure out how to turn these derived variables (Year and Session) into numeric, so that I can then get proper summaries and use the "subset" function.

##Generate sample dataset
df=data.frame(StudyAreaVisitNote=c("2006 Session 1","2006 Session 2", "2008 Session 4", "2012 Session 3"))

##Create new column denoting year and session on their own
as.factor(df$StudyAreaVisitNote)
df$Year <- substr(x = df$StudyAreaVisitNote, start = 1, stop = 4)
df$Session <- substr(x = df$StudyAreaVisitNote, start = 13, stop = 14)

##Summary of Data
summary(df)  ## Year and Session are Class and Mode "Character", summary provides little info

##Turn Year and Session into Numeric
as.numeric(df$Year)
as.numeric(df$Session)


##Try Summary of Data Again
summary(df)  ## Again, Year and Session are Class and Mode "Character", summary provides little info
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The lines

as.factor(df$StudyAreaVisitNote)
as.numeric(df$Year)
as.numeric(df$Session)

do not permanently change the values in df. They return transformed vectors that are printed to the console, then, because you do not save them anywhere, they disappear as soon as that line in done being called. Generally objects in R are not updated via referece, you must alwayts re-assign the returned result to wherevver you would like to store it. So try

df$Year <- as.numeric(df$Year)
df$Session <- as.numeric(df$Session)

instead


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...