R - error in for loop [closed] - r

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I want to do a for loop with lists.
I tried :
for(tri in tripletsFinaux){
CoefsCrit = apprentissage(data,tri$concurrents,tri$client,tri$depot)
/* I put actions here but you don't need to see it for my problem */
}
In my loop, I launch a function which need the different values of the list tri.
Because tripletsFinaux is a dataframe. And I need the 3 values of each tri to perform my function apprentissage() in the for loop.
tripletsFinaux looks like :
head(tripletsFinaux,2)
depot client concurrents nbLignes
1 blablabla blobloblo tatata 131
2 bliblibli blublbublu tututu 231
My error is :
$ operator is invalid for atomic vectors
What can I do ?
I don't know if the error is in the apprentissage() function or in the for loop

It seems like you want to loop over the rows, so:
for(i in 1:nrow(tripletsFinaux)){
CoefsCrit = apprentissage(data, tripletsFinaux$concurrents[i], tripletsFinaux$client[i], triplentsFinaux$depot[i])
# ...
}
The above is what you want. The way you have it, for (tri in tripletsFinaux) will loop over the columns, one column at a time. I was hoping that would be clear if you ran for (tri in tripletsFinaux) {print(head(tri))}.

Related

Subset data in R to look at specefic country [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am currently using the EVS data. It is a panel data from 1981-2021. This has 223099 obvs and 635 variables. One of the variables of interest is the country. I am trying to subset the data frame to only look at one country but at the same time look at the other variables. I am not sure what to do.
I input the code:
data <-subset(data,COW_NUM == "339")
Where COW_NUM is the country number. 339 is the country of interest
I keep getting an error message. I don't know what I am doing wrong.
Can you provide a link to the data?
my_data: this is your data
my_data$COW_NUM: this is the column that you want to use to filter
In R if you do my_data$COW_NUM == "339" ("339" is is string ?), R will return a vector of TRUE/FALSE value (223099 TRUE/FALSE if I understand correctly). R will check if each value of my_data$COW_NUM == 339 if yes it return TRUE if not FALSE.
Then you can use this new vector inside [ to subset my_data:
my_data[my_data$COW_NUM == "339",] will keep every rows of my_data where my_data$COW_NUM == "339" give a TRUE and discard the one with FALSE.
Last step should be:
my_data_339 <- my_data[my_data$COW_NUM == "339",]
Hope it help but it is hard to do it without the data!

Errors in Executing While loop [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
I am trying to read a random data from a dataset in while loop but I'm getting errors, can anyone here help me?
How to calculate the percentage of points in the sample that are greater than 100?
I tried following method
dataset = 1:100
i=0
while(dataset[i] > condition) #compare every value in dataset
{
percent_age= dataset[i] + percent_age
i=i+1
if(i=100)
{break}
}
But it gives me only errors.
The while statement is evaluated before anything in the body, so the first time it is evaluated i is equal to 0 and so dataset[i] is dataset[0] which is an empty object (vector of length 0), you also have not defined condition in the code that you give us. So while is looking of a single logical value, but you are giving it the result of comparing a zero-length vector to an undefined value. That is going to give at least one error.
You can fix that by starting i at 1 and defining condition before the while.
In your if statement you have i=100, that is setting i to 100, to compare (and return a logical) it should be i == 100.
Because R can be used interactively, it tries to evaluate code as early as possible, therefore it is best to put the opening curly bracket { on the same line as the keywords like if and while.
A couple of nit-picky things that probably will not resolve errors, but could help for better programming in the future:
Use more whitespace within lines: i = i + 1 can be easier to read than i=i+1 and mistakes like i=100 vs i == 100 are easier to catch when whitespace is used appropriately.
I find the arrow assignment in R i <- 1 reads easier and lessens chances of confusing different uses of =, so I would recommend using it for all assignments.

I want to remove duplicated entries in a data.frame based on one column [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
uniqueIDs <- data.frame(unique(MASTERFILE$Number), MASTERFILE[,3])
So I have this big table called "MASTERFILE". In this table, I have a column called "Number" which has a number for each row. Some of the rows will have the same number, so for instance:
1
2
3
3
4
5
So what I would like to do, is to remove the duplicate "3" entry. However, I also want column number 3 to be included in my new "uniqueIDs" data frame (hence the MASTERFILE[,3] part).
Unfortunately, when I try to run this, it will say that the rows from the unique function are different from the rows in column 3, which is obvious, however the question now is, how can I make sure those same rows that where removed in the unique function, also get removed in the 3rd column?
I am sure this questions has already been asked and answered, but something like this should work:
MASTERFILE[!duplicated(MASTERFILE$Number), c("Number", "Col3")]

for loop with dimension of a list [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a code,and i am going to do a for loop on two gamma distributions.
Given the list of shape parameter, and i name them "d" then i put in d[1] and d[2] in the random gamma function.
I have simplified what I wish to ask here. when i code d[1] in R, output will be the first vector,while when i code d[2] in R, output will be the second vector.
I have a bit lose then how will it iterate if i using for loop for d ?
*
List_1 <- list(c(4,16),c(16/9,4),c(1,16/9),c(.64,1),c(4/9,.64))
for ( d in List_1) ##first parameter is for gamma.1, second is for gamma.2
{
x<-rgamma(25,d[1],1)
y<-rgamma(25,d[2],1)
t<-t.test(x,y)$p.value
}*
I am sorry if i do ask a silly question. Thanks in advance.
In R it is better to avoid for loops due to their poor performance. Since you are starting with a list lapply is a good start:
lapply(List_1, FUN=function(x){t.test(rgamma(25,x[1],1), rgamma(25,x[2],1))$p.value})
The apply function takes your list and then uses the gamma function on the 2 parameters within the t.test. The result will be a list of the five p values, one for each pair
Your code runs fine. I am actually not sure what you are asking here. You can just use print to find the iteratives, if that is what you want, like:
for (d in List_1){
print(d[1])
print(d[2])
}

why my logic is not working to create functions? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I want to create a function that to read differents sheets from a worksheet of a file in Excel,I have tried many different ways to do this but I keep runnning into errors, currently, this seems to be simple:
hoja <- readWorksheetFromFile("data.xls",sheet=1)
x <- c("lamb","GLU")
hoja1 <- hoja[hoja[,"time"]==-5,x]
After that, I tried to generalized it by creating this function:
submatrix <- function(file, sheet,time,col){
hoja <- readWorksheetFromFile(file,sheet=sheet)
hoja1 <- hoja[ hoja[,"time"] == time,col]
}
what does mean this error...it is not make sense for me...
Error: object 'hoja' not found
> }
Error: unexpected '}' in "}"
Does anyone have some suggestion for me? it isnt the way to create functions?
You're probably accessing hoja outside that function, while you have not returned anything at all (so the last item only will be returned), causing all items but the last you have defined to be destroyed after the function finishes its work.
Try this.
submatrix <- function(file, sheet,time,col){
hoja <- readWorksheetFromFile(file,sheet=sheet)
hoja1 <- hoja[ hoja[,"time"] == time,col]
list(hoja, hoja1)
}
You can then access those items as follows:
item <- submatrix(...)
item[[1]] #refers to hoja
item[[2]] #refers to hoja1

Resources