i am unable to add column values in R in my DF. after reading the CSV, I need to calculate the individual cell values which have two values. How do I go about doing that?
            Asked
            
        
        
            Active
            
        
            Viewed 39 times
        
    0
            
            
        - 
                    1Welcome to Stack Overflow. We cannot read data into R from images. Please [make this question reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by including a small representative dataset in a plain text format - for example the output from `dput(yourdata)`, if that is not too large. – neilfws Nov 28 '21 at 23:37
- 
                    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Do not post picture of data because we can't copy/paste that into R. – MrFlick Nov 29 '21 at 06:50
- 
                    you need to look up sub setting a data.frame. But bear in mind you don't have any proper numbers in the columns except for the first one. And, as everyone says, please provide a reproducible example. – DarrenRhodes Dec 02 '21 at 14:39
- 
                    Please provide enough code so others can better understand or reproduce the problem. – Community Dec 02 '21 at 14:40
1 Answers
1
            
            
        Here are two solutions with toy data:
df <- data.frame(
  Variable = c("1-3", "40-45")
)
with sapply:
library(stringr)
sapply(lapply(str_extract_all(df$Variable, "\\d+"), as.numeric), sum)
[1]  4 85
with map_dbl:
library(purrr)
library(stringr)
library(dplyr)
df %>%
  mutate(
    # extract whatever digits are there:
    add = str_extract_all(df$Variable, "\\d+"),
    # map the digits to one another and add the first to the second:
    add = map_dbl(add, function(x) as.numeric(x)[1] + as.numeric(x)[2]))
  Variable add
1      1-3   4
2    40-45  85
 
    
    
        Chris Ruehlemann
        
- 20,321
- 4
- 12
- 34
 
    