dsnOwhiskey - 11 months ago 61

R Question

I'm trying to sum the digits of integers in the last 2 columns of my data frame. I have found a function that does the summing, but I think I may have an issue with applying the function - not sure?

`Dataframe`

a = c("a", "b", "c")

b = c(1, 11, 2)

c = c(2, 4, 23)

data <- data.frame(a,b,c)

#Digitsum function

digitsum <- function(x) sum(floor(x / 10^(0:(nchar(as.character(x)) - 1))) %% 10)

#Applying function

data[2:3] <- lapply(data[2:3], digitsum)

This is the error that I get:

`*Warning messages:`

1: In 0:(nchar(as.character(x)) - 1) :

numerical expression has 3 elements: only the first used

2: In 0:(nchar(as.character(x)) - 1) :

numerical expression has 3 elements: only the first used*

Answer Source

Your function `digitsum`

at the moment works fine for a single scalar input, for example,

```
digitsum(32)
# [1] 5
```

But, it can not take a vector input, otherwise `":"`

will complain. You need to vectorize this function, using `Vectorize`

:

```
vec_digitsum <- Vectorize(digitsum)
```

Then it works for a vector input:

```
b = c(1, 11, 2)
vec_digitsum(b)
# [1] 1 2 2
```

Now you can use `lapply`

without trouble.