R检查数字的字符值并自动更改var数据类型(R check character values for numeric and change var datatype automatically)

编程入门 行业动态 更新时间:2024-10-20 11:49:28
R检查数字的字符值并自动更改var数据类型(R check character values for numeric and change var datatype automatically)

我有许多数据帧,其中所有数据都是字符。 我猜可以将包含数字的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 2

We 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

更多推荐

本文发布于:2023-08-06 06:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1446068.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据类型   字符   数字   check   character

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!