将类和模式从字符更改为数字 [英] 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
推荐答案
行
as.factor(df$StudyAreaVisitNote)
as.numeric(df$Year)
as.numeric(df$Session)
不要永久更改 df
中的值。它们返回转换后的矢量,这些矢量将打印到控制台,然后,因为您没有将它们保存在任何地方,所以一旦调用该行,它们就会消失。通常,R中的对象不会通过引用进行更新,您必须始终将返回的结果重新分配到您想要存储它的位置。因此,请尝试
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)
相反
这篇关于将类和模式从字符更改为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!