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

4037 views r
0

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

answered question

Your code is working fine on my side

For me this is working fine.

it's not working with me .. I don't know where the problem is :-S

what is the result you get ? Restart your R session to have a clean session, and retry. Your code is correct.

1 Answer

0

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.

posted this

Have an answer?

JD

Please login first before posting an answer.

Related

Ads

Categories