# (-1)^k for each k from 0 to 9 in R

I'm new to R and I want to create a vector x, length 10 in this case that gives (-1)^k for each k = 0,...,9 in R it's supposed to give this [1] 1 -1 1 -1 1 -1 1 -1 1 -1 but it's not working I tried this but it doesn't work ..any advice?

```
b<- 0:9
x<- (-1)**b
x
# this should be the answer
[1] 1 -1 1 -1 1 -1 1 -1 1 -1
```

### 1 Answer

You are almost there. And you are right that R will iterate over lists and arrays. If you are having issues getting it to work try expressly creating an array of b

```
b<- c(0:9)
```

This definitely works in all R environments.

sconfluentus
posted this

## Have an answer?

JD

Your code is working fine on my side