我有许多数据帧,其中所有数据都是字符。 我猜可以将包含数字的var更改为数字数据类型。 虽然我有100个列,所以我不想输入每个列进行更改以便更改它。 是否有另一种方法可以自动执行此过程并扫描一列数据,检查字符是否具有数字值并将其从字符类型更改为数字类型?
employee <- c('John Doe','Peter Gynn','Jolie Hope') salary <- c("21000", "23400", "26800") gender <- c("M", "M", "F") rank <- c("5", "109", "2") df <- data.frame(employee, salary, gender, rank)我不想为每个列/ var执行此操作
df$rank <- as.numeric(df$rank)我想做这样的事情
i <- sapply(df, is.vector.of.columns.contaning.numeric.values) df[i] <- lapply(df[i], as.numeric)I have many dataframes where all the data is character. I can guess that a var containing a number should be changed to a numeric data type. I have 100's of columns though so I don't want to type out each each one to change in order to change it. Is there another way to automate this process and to scan a column of data check if the character has a numeric value and change it into a numeric type from character type?
employee <- c('John Doe','Peter Gynn','Jolie Hope') salary <- c("21000", "23400", "26800") gender <- c("M", "M", "F") rank <- c("5", "109", "2") df <- data.frame(employee, salary, gender, rank)I don't want to have to do this for each column/var
df$rank <- as.numeric(df$rank)I would like to do something like this
i <- sapply(df, is.vector.of.columns.contaning.numeric.values) df[i] <- lapply(df[i], as.numeric)最满意答案
我们可以用数字条件写一个函数。 它的工作原理是尝试as.numeric并检查它是否返回NA ,如果是,则表示该值不能被强制转换为明确的数字。 发生这种情况时,该函数将保持列不变。
smartConvert <- function(x) { if(any(is.na(as.numeric(as.character(x))))) x else as.numeric(x) } df[] <- lapply(df, smartConvert) str(df) # 'data.frame': 3 obs. of 4 variables: # $ employee: Factor w/ 3 levels "John Doe","Jolie Hope",..: 1 3 2 # $ salary : num 1 2 3 # $ gender : Factor w/ 2 levels "F","M": 2 2 1 # $ rank : num 3 1 2We can write a function with the number condition. It works by trying as.numeric and checking if it returns NA, if it does, that means the value cannot be coerced to an unambiguous numeric. When this happens, the function will keep the column as is.
smartConvert <- function(x) { if(any(is.na(as.numeric(as.character(x))))) x else as.numeric(x) } df[] <- lapply(df, smartConvert) str(df) # 'data.frame': 3 obs. of 4 variables: # $ employee: Factor w/ 3 levels "John Doe","Jolie Hope",..: 1 3 2 # $ salary : num 1 2 3 # $ gender : Factor w/ 2 levels "F","M": 2 2 1 # $ rank : num 3 1 2更多推荐
发布评论