Stack Overflow Asked by hidden-layer on November 20, 2021
I have a table like this,
structure(list(component = structure(1:4, .Label = c("a", "b",
"c", "d"), class = "factor"), dimension = structure(c(1L, 3L,
4L, 2L), .Label = c("12*10*05", "30*30*20(2)+32*34*04", "30*30*20+174*153*21+108*014*04",
"98*98*12(2)"), class = "factor")), class = "data.frame", row.names = c(NA,
-4L))
I need to perform mathematical operation in column ‘dimension’ which is stored as character and convert to integer. For ex: "98*98*12(2)"
should be stored as a result of =(98*98*12)*2 = 230496
The desired output:
structure(list(component = structure(1:4, .Label = c("a", "b",
"c", "d"), class = "factor"), dimension = c(600L, 583110L, 230496L,
40352L)), class = "data.frame", row.names = c(NA, -4L))
You can try the code below
> within(df, new.dimension <- sapply(gsub("(\d)\(","\1*(",dimension),function(x) eval(parse(text = x))))
component dimension new.dimension
1 a 12*10*05 600
2 b 30*30*20+174*153*21+108*014*04 583110
3 c 98*98*12(2) 230496
4 d 30*30*20(2)+32*34*04 40352
Answered by ThomasIsCoding on November 20, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP