add 3 columns to imported matrix

Hello
Could someone provide me the code how to add 3 columns to an imported matrix?

The first column should show the rolling Alpha of every ongoing rolling value (based on 10 previous values)
The second column should give me the Beta of every ongoing rolling value (based on 10 previous values)
The third column should give me the Gamma of every ongoing rolling value (based on 10 previous values)

enclosed picture .
Cheers ,Ron

Can you provide a reproducible example? Screenshots aren't useful.

Are you trying to difference the time series vales?

What have you tried so far? what is your specific problem?, we are more inclined towards helping you with specific coding problems rather than doing your work for you.

Could you please turn this into a self-contained REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.

Hi, indeed , I dont expect another do the work :slight_smile:
I will show what coding i have so far and post that in a separate posting.
Thanks for your support.

cheers

Hi William
Thank you for your info.
I have the following reprex

matrix
|1|03-01-2017|40|
|--seq-|-date--|-sales--|
|2|04-01-2017|2|
|3|05-01-2017|2|
|4|06-01-2017|2|
|5|07-01-2017|30|
|6|08-01-2017|2|
|7|01-02-2017|9|
|8|02-02-2017|5|
|9|03-02-2017|65|
|10|04-02-2017|3|
|11|05-02-2017|65|

#step 1:Import the file

sample_reprex <- read_excel("E:/Forecast calcs/sample reprex.xls")
View(sample_reprex)

#step 2 2.a add column ,
#( this coding does not work :frowning: )
add_column(df, z = -1:1, w = 0)

step 2b
fill that extra column with Chronbachs alpha calculation
#The Cronbach's alpha computed by cronbach . alpha () is defined as follows α =pp−1(1−∑pi=1σ2yiσ2x), where p is the number of items σ2x is the variance of the observed total test scores, and σ2yi is the variance of the ith item. The standardized Cronbach's alpha computed by cronbach .

coding : psych::alpha(sample_reprex)

hope the above helps
question : what have i done wrong to add that extra column and have Chronb Alpha calc in there ,per line.

cheers

You didn't get the reprex correct as shown in the article. It just makes it easier for other people to help you if you do.

I assume that it should be something like this:

df <- tibble::tribble(
  ~seq,       ~date, ~sales,
     1, "3/01/2017",     40,
     2, "4/01/2017",      2,
     3, "5/01/2017",      2,
     4, "6/01/2017",      2,
     5, "7/01/2017",     30,
     6, "8/01/2017",      2,
     7, "1/02/2017",      9,
     8, "2/02/2017",      5,
     9, "3/02/2017",     65,
    10, "4/02/2017",      3,
    11, "5/02/2017",     65
  )

Which is:

# A tibble: 11 x 3
     seq date      sales
   <dbl> <chr>     <dbl>
 1     1 3/01/2017    40
 2     2 4/01/2017     2
 3     3 5/01/2017     2
 4     4 6/01/2017     2
 5     5 7/01/2017    30
 6     6 8/01/2017     2
 7     7 1/02/2017     9
 8     8 2/02/2017     5
 9     9 3/02/2017    65
10    10 4/02/2017     3
11    11 5/02/2017    65

Then, this bit won't work because there z only has three values (-1, 0, 1) but you have 11 rows (or more in the real dataset).

add_column(df, z = -1:1, w = 0) # this won't work
add_column(df, z = 0, w = 0) # this will

Which gets you here:

# A tibble: 11 x 5
     seq date      sales     z     w
   <dbl> <chr>     <dbl> <dbl> <dbl>
 1     1 3/01/2017    40     0     0
 2     2 4/01/2017     2     0     0
 3     3 5/01/2017     2     0     0
 4     4 6/01/2017     2     0     0
 5     5 7/01/2017    30     0     0
 6     6 8/01/2017     2     0     0
 7     7 1/02/2017     9     0     0
 8     8 2/02/2017     5     0     0
 9     9 3/02/2017    65     0     0
10    10 4/02/2017     3     0     0
11    11 5/02/2017    65     0     0

This is probably a homework assignment for you, but the instructions are horrible. You should also read the reprex article as stated if you are hoping for help.

Hi, thats great advise. I have checked it out and when I copy from excel to tibble:

datapasta::dpasta()
datapasta()
install.packages("datapasta")
tibble::tribble(
~Date, ~No1,
"03-01-2017", 40L,
"04-01-2017", 2L,
"05-01-2017", 2L,
"06-01-2017", 2L,
"07-01-2017", 30L,
"08-01-2017", 2L,
"01-02-2017", 9L,
"02-02-2017", 5L,
"03-02-2017", 65L,
"04-02-2017", 3L,
"05-02-2017", 65L,
"06-02-2017", 1L,
"07-02-2017", 1L
)
add_column(df, z = 0, w = 0)

#I have the above in the console and have the following error message
Error in add_column(df, z = 0, w = 0) :
could not find function "add_column"

what did i do incorrect?
cheers

as you can see i suddenly have a character 'L" appearing out of nowhere...

The 'L' just indicates that it is an integer.

add_column is in the tibble package.

1 Like

this is it :slight_smile: it worked. awesome support.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.