简体   繁体   中英

convert string column values to numeric and find maximum in those numeric values in R

I have a column called "XYZ" (XYZ is one of the column in my data frame) in data frame and this "XYZ" column is a string type. values of the "XYZ" column is like below

example:

   XYZ
new_value_1
new_value_2
new_value_4
new_value_3

I have to get the last digit(which is a number) and convert that into number and finds the maximum among those number. After finding maximum number in that column I need to generate a sequence from that maximum number till n rows.

For example from the above "XYZ" every string has digit at the end I have to get the last digit which is number and finds the maximum in those numbers, in this case maximum is 4 after finding maximum I have to mutate id column and id will starts from next number to the maximum number.

output:

 XYZ             ID
new_value_1      5
new_value_2      6
new_value_4      7
new_value_3      8

In the future, please make a minimally reproducible input data set using dput. I've recreated the data set for convenience.

Using the dplyr package for ease:

library(dplyr)
raw_data <- data.frame("XYZ"= c("new_value_1","new_value_2","new_value_3","new_value_4"))

##get the max value
max_value <- max(sapply(raw_data$XYZ, function(x){as.numeric(strsplit(x, "_")[[1]][3])}))

#make the resulting data

final_data <- raw_data %>% mutate(ID = (max_value+1):(max_value+nrow(raw_data)))

Let me know if dplyr is not allowed.

Here is a base R way. It uses a regex to extract the last digit or digits and seq.int to create a sequence like the sequence in the question.

m <- max(as.integer(sub("^[^[:digit:]]*([[:digit:]]+$)", "\\1", df1$XYZ)))
df1$ID <- m + seq.int(nrow(df1))

df1
#          XYZ ID
#1 new_value_1  5
#2 new_value_2  6
#3 new_value_4  7
#4 new_value_3  8

Data

df1 <- read.table(text = "
   XYZ
new_value_1
new_value_2
new_value_4
new_value_3
", header = TRUE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM