R studio - which purrr function to iterate - loops

I am a beginner so I am confused as to how to create a vector of alphas to contain the values of α so that they match the individual ids in the ids vector.
I know only that I have to iterate it using a function (alpha_df) from purrr

Related

formula to generate array with variables such as =B1/B2, etc

How does one generate an array whose elements are cell values or math operation of cell values? For example, how does one generate an array of {0,1,A1,B1}) or {8,3,A1/B1,A1*B1} or {0,1,A1/B1-2, A1*B1+3}?
I have not found anything on the net regarding this topic.

How to Create a table in C that consists of Arrays of fixed lenght

I am trying to implement a search table that basically consists of arrays of a fixed size. In my case each array would consist of 4 elements (Say for ex letters W,X, Y and Z). I need each element in the array to have a fixed index in the table using which it can be found and accessed by the user. The table would something like this..( the symbol | is used below to show the ending of that particular array)
WXYZ|XYWZ|WXZY|....|.... and so on
Could somebody tell me which is the best way to implement htis? I have heard of linked lists and hash tables but I am not sure if that is the best method to do this..
I can't get it, why don't you just use an ordinary two dimensional array like array[100][4], the user can access through an ordered pair of two indexes.

Find the subset of an array such that the elements in the subset have a common difference

I came across a question while preparing for my interview.
Given an array of integers as input.
We have find a possible subset such that the elements in the array have a common difference.
For example,
Consider the input array to be {1,3,7,10,11}
Then the output should be {3,7,11}.
It is always that the elements in the array are in increasing order.
I thought of finding all the subsets and look for a solution.
But that would cause my program to run slower if the input array size is too large.
can anyone help me to crack this???
From what I understand, you want to extract possible subsets from an array such that each two consecutive numbers have the same difference value increasingly.
Here is my algorithm:
Remove duplicates.
Force arrange ascendingly.
Keep a hashtable with the difference values as keys and lists of lists as values.
Loop through the array, updating/adding a key in the hashtable that equals the difference between the two consecutive numbers at hand, and adding a list to the value (the list of lists) containing the two numbers.
After the loop, create an array. Loop through the hashtable, adding an element each time to the array which is an array itself: The merging of all nested lists in the value at hand. This is the array containing all possible subsets.
Here's a possible implementation in python:
from itertools import chain
def find_subsets (array):
table = dict()
last = array[-1]
for num in sorted (set (array), False)[1:]:
diff = last - num
table[diff].append([num, last])
last = num
return [list(chain(v)) for k, v in table]
Please try this code and correct it if wrong. I wrote this in a hurry.

Searching for identical values in two arrays

I know this may seem trivial to most/all of you, but I've been scratching my head over this for some time now.
I want to find values in the first column (values in the first column of each array are 'years') of 2 separate arrays that are identical in size (2400 x 2). Once such values have been found, I am trying to store the corresponding values in column 2 of both arrays in a new array called z. My code is as follows:
n=1;
a_years=austrianAtmosphericTemperaturesTest(n,1);
r_years=frenchRainDataSimplified(n,1);
while n<=2400
for a_years = r_years
z=[frenchRainDataSimplified(n,1) austrianAtmosphericTemperaturesTest(n,2) frenchRainDataSimplified(n,2)];
n=n+1;
end
end
I have tried other methods like find & ismember but I'm having no luck!
Thanks,
Chris

How to get mean, median, and other statistics over entire matrix, array or dataframe?

I know this is a basic question but for some strange reason I am unable to find an answer.
How should I apply basic statistical functions like mean, median, etc. over entire array, matrix or dataframe to get unique answers and not a vector over rows or columns
Since this comes up a fair bit, I'm going to treat this a little more comprehensively, to include the 'etc.' piece in addition to mean and median.
For a matrix, or array, as the others have stated, mean and median will return a single value. However, var will compute the covariances between the columns of a two dimensional matrix. Interestingly, for a multi-dimensional array, var goes back to returning a single value. sd on a 2-d matrix will work, but is deprecated, returning the standard deviation of the columns. Even better, mad returns a single value on a 2-d matrix and a multi-dimensional array. If you want a single value returned, the safest route is to coerce using as.vector() first. Having fun yet?
For a data.frame, mean is deprecated, but will again act on the columns separately. median requires that you coerce to a vector first, or unlist. As before, var will return the covariances, and sd is again deprecated but will return the standard deviation of the columns. mad requires that you coerce to a vector or unlist. In general for a data.frame if you want something to act on all values, you generally will just unlist it first.
Edit: Late breaking news(): In R 3.0.0 mean.data.frame is defunctified:
o mean() for data frames and sd() for data frames and matrices are
defunct.
By default, mean and median etc work over an entire array or matrix.
E.g.:
# array:
m <- array(runif(100),dim=c(10,10))
mean(m) # returns *one* value.
# matrix:
mean(as.matrix(m)) # same as before
For data frames, you can coerce them to a matrix first (the reason this is by default over columns is because a dataframe can have columns with strings in it, which you can't take the mean of):
# data frame
mdf <- as.data.frame(m)
# mean(mdf) returns column means
mean( as.matrix(mdf) ) # one value.
Just be careful that your dataframe has all numeric columns before coercing to matrix. Or exclude the non-numeric ones.
You can use library dplyr via install.packages('dplyr') and then
dataframe.mean <- dataframe %>%
summarise_all(mean) # replace for median

Resources