Disclaimer: Save the odd script or two, I'm still fairly new to R/coding, AND this is my first post here ever, so please be gentle. I apologize for how vague things may be, I simply don't have the coding vocabulary at this point in time to best describe what I'm trying to do.
Anyway:
I have a dataframe where column b
describes the amount of cookies, x
, left in a jar and column a
contains the names of all the jars, y
, from which the cookies were taken.
This dataframe needs routine updating due to the high volume and frequency of cookies taken from many different jars.
As of now, I have a script that will pull from a master list of jars and cookies and write a csv containing only jars required for the order and how many cookies are left in each jar.
I would like to add some readline(prompt)
that will ask the user something like "Amount of cookies taken from Jar z
: " for each Jar in the order and then update that exact cell in the master list of jars and cookies with the provided user input.
I know how to do everything I've mentioned here EXCEPT how to tell R to include z
in the user prompt.
So, in other terms, if I have an order form where someone requested cookies from jars 5, 11, and 31 how do I tell R to prompt:
'Amount of cookies taken from JAR 5: '
wait for input
'Amount of cookies taken from JAR 11: '
wait for input
'Amount of cookies taken from JAR 31: '
wait for input
Here is an example of how you can do it using a for
loop.
1) we can define a dummy dataframe:
df <- data.frame(Jars = LETTERS[1:5],
Cookie = sample(1:50,5))
So your dataframe looks like:
> df
Jars Cookie
1 A 9
2 B 17
3 C 23
4 D 34
5 E 19
2) Then, the code for iterating through the Jars
and ask for the input for each and substract it from Cookie
column (you can even print the current amount of cookies in each jar in order to avoid having negative results ;)):
for(i in 1:nrow(df))
{
x <- as.numeric(readline(paste0("How many in Jar ",df$Jars[i]," (current number:",df$Cookie[i],") : ")))
df[i,"Cookie"] <- df[i,"Cookie"] - x
}
3) Now in action:
> for(i in 1:nrow(df))
+ {
+ x <- as.numeric(readline(paste0("How many in Jar ",df$Jars[i]," (current number:",df$Cookie[i],") : ")))
+ df[i,"Cookie"] <- df[i,"Cookie"] - x
+ }
How many in Jar A (current number:9) : 0
How many in Jar B (current number:17) : 2
How many in Jar C (current number:23) : 1
How many in Jar D (current number:34) : 3
How many in Jar E (current number:19) : 0
4) And now your dataframe looks like:
> df
Jars Cookie
1 A 9
2 B 15
3 C 22
4 D 31
5 E 19
Does it correspond to what you are looking for ?