To calculate expected value in R, there two different ways are available in R.

The following methods show how you can do it with syntax.

**Method 1: Use sum() Function**

```
sum(data*probability)
```

**Method 2: Use mean() Function**

```
mean(data)
```

The following example shows how to calculate expected value in R.

## Using sum() Function

Let’s see how to use **sum()** function to calculate expected value when we have sample data and probability values:

```
# Define values
data <- c(12,23,34,45,56,67)
# Define probability values
probability <- c(0.1,0.2,0.3,0.4,0.5,0.6)
# Calculate expected value
sum(data*probability)
```

Output:

```
[1] 102.2
```

Here the output shows expected value which calculated using sample data and probability values.

## Using mean() Function

Let’s see how to use **mean()** function to calculate expected value:

```
# Define values
data <- c(12,23,34,45,56,67)
# Calculate expected value
mean(data)
```

Output:

```
[1] 39.5
```

Here the output shows expected values which calculated using **mean()** function.