Looping error in R

0 votes
g = 9.8
tt = c(10,15,16)
n = length(tt)
emp =NULL

for(i in n){
emp[i] = tt[i]*4
print(emp)
}

After running this code in R, i got reply like below:

 > for(i in n){
 + emp[i] = tt[i]*4
 + print(emp)
 + }
[1] NA NA 64

Any ideas about how to remove NA values or solve this problem?

Oct 30, 2018 in Data Analytics by Ali
• 10,430 points
23 views

1 answer to this question.

0 votes

You need a sequence to iterate over for your loop to work. By simply changing n to 1:n it will work.

g = 9.8
tt = c(10,15,16)
n = length(tt)
emp =NULL

for(i in 1:n){
  emp[i] = tt[i]*4
  print(emp)
}

[1] 40
[1] 40 60
[1] 40 60 64
answered Oct 30, 2018 by Maverick
• 10,040 points

Related Questions In Data Analytics

0 votes
1 answer
0 votes
1 answer

Error in source("myfunction.R") : myfunction.R:2:3: unexpected symbol 1: 2: R version

Try formatting your code like this: myfunction <- ...READ MORE

answered Oct 30, 2018 in Data Analytics by Maverick
• 10,040 points
277 views
0 votes
1 answer

"Error in if" while trying to execute simple code in R

This caused non-logical data or missing values passed ...READ MORE

answered Oct 31, 2018 in Data Analytics by Kalgi
• 42,330 points
40 views
0 votes
1 answer

"no applicable method" Error in r programming

This is caused by using an object-oriented ...READ MORE

answered Oct 31, 2018 in Data Analytics by Kalgi
• 42,330 points
98 views
0 votes
1 answer

By using dpylr package sum of multiple columns

Basically here we are making an equation ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,720 points
114 views
0 votes
1 answer

How to convert a text mining termDocumentMatrix into excel or csv in R?

By assuming that all the values are ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,720 points
158 views
0 votes
1 answer

In a dpylr pipline how to use sample and seq?

For avoiding rowwise(), I prefer to use ...READ MORE

answered Apr 6, 2018 in Data Analytics by DeepCoder786
• 1,720 points
85 views
0 votes
1 answer

How to create a list of Data frames?

Basically all we have to do is ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,720 points
99 views
0 votes
1 answer

Error saying "Error in rnorm() : argument "n" is missing, with no default" in R

Hey @ali, rnorm() function requires an argument, ...READ MORE

answered Oct 30, 2018 in Data Analytics by Maverick
• 10,040 points
284 views
0 votes
1 answer

"Error in eval(ei, envir) : object 'RDX2' not found" when trying to source the code in R

This is a very common issue that ...READ MORE

answered Oct 30, 2018 in Data Analytics by Maverick
• 10,040 points
787 views