我有以下data.frame:
employee <- c('John Doe','Peter Gynn','Jolie Hope')
# Note that the salary below is in stringified format.
# In reality there are more such stringified numerical columns.
salary <- as.character(c(21000, 23400, 26800))
df <- data.frame(employee,salary)
输出是:
> str(df)
'data.frame': 3 obs. of 2 variables:
$ employee: Factor w/ 3 levels "John Doe","Jolie Hope",..: 1 3 2
$ salary : Factor w/ 3 levels "21000","23400",..: 1 2 3
我想要做的是将值从字符串直接转换为df
变量的纯数字。同时保留字符串名称employee
。我试过这个但不会工作:
as.numeric(df)
归根结底,我想对来自df
. 比如df2 <- log2(df)
等。