Matías de Felipe - 1 year ago 76

R Question

I have three different treatments and within each one I have five sample values. I am into difficulties in how to sum the five values corresponding at each treatment.

Really I am working with a big dataframe which management is not too easy for me.

Below there is the dataframe

`dataexample_2`

`Treatment Variable`

A1 0.020408163

A1 0.040816327

A1 0.06122449

A1 0.081632653

A1 0.102040816

A2 0.12244898

A2 0.142857143

A2 0.163265306

A2 0.183673469

A2 0.204081633

A3 0.224489796

A3 0.244897959

A3 0.265306122

A3 0.285714286

A3 0.306122449

names(dataexample_2)

summary(dataexample_2)

Add<-sapply(split(dataexample_2, dataexample_2$Treatment), function(d) sum(dataexample_2$Variable,na.rm = FALSE))

Table<-as.data.frame(Add)

I am obtaining:

`Add`

A1 2.44898

A2 2.44898

A3 2.44898

And I wish to obtain:

`Add`

A1 0.3061

A2 0.8163

A3 1.3265

Any help will be really appreciated. Thanks in advance. Regards. MatÃas.

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

The problem with your code is that you refer to the whole data in your anonymus `function(d)`

instead of the current data `d`

.

The following corrected line does what you want:

```
sapply(split(dataexample_2, dataexample_2$Treatment), function(d) sum(d$Variable,na.rm = FALSE))
```

Also I would suggest that you have a look at `tapply`

which is better suited for the task you want to accomplish than `sapply`

. It can do the whole thing much clearer:

```
tapply(dataexample_2$Variable, dataexample_2$Treatment, sum)
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**