[英]Replacing character strings (values) in a column in R with integers in an efficient (vectorized) way
I have a big dataframe and I want to replace the values in a column which are character strings with integers. 我有一个大数据框,我想用整数替换一列中的值。 There are more than one thousand unique strings in this column.
此列中有超过一千个唯一字符串。 One way is through a for loop.
一种方法是通过for循环。 Here is the code with a toy dataset:
这是带有玩具数据集的代码:
data(mtcars)
library(stringr)
mtcars$gear = as.character(mtcars$gear)
unique_values = unique(mtcars$gear)
for (i in (1:length(unique_values))){
mtcars$gear = str_replace(string = mtcars$gear, pattern = unique_values[i], replacement = as.character(i))
}
Is there a more efficient way? 有没有更有效的方法?
Using the dplyr
library should do the trick: 使用
dplyr
库应该可以解决问题:
data(mtcars)
library(stringr)
library(dplyr)
mtcars$gear = as.character(mtcars$gear)
mutate(mtcars, gear = as.integer(gear))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.