logo
down
shadow

Finding values in a matrix from list of values in R


Finding values in a matrix from list of values in R

By : Denis S
Date : October 19 2020, 08:10 PM
I wish did fix the issue. Could somebody please point me in the right direction? I have a list of results (df2 below) and I want to use those to find the values from the matrix (df below). , Here is a base R way
code :
mat <- cbind(match(df2$V1, df$Date), match(df2$V2, names(df)))
df2$results <- df1[mat]
df2
#         V1    V2 results
#1 24-Sep-18 Team1      18
#2 14-Sep-18 Team2       9
#3 10-Sep-18 Team3      11
#4 21-Sep-18 Team4      11 # your expected seems to be wrong here
#5 20-Sep-18 Team5      11
#6  7-Sep-18 Team6       1


Share : facebook icon twitter icon
Finding mean of groups of values within a matrix

Finding mean of groups of values within a matrix


By : saeid TALA
Date : March 29 2020, 07:55 AM
it should still fix some issue I am trying to take the mean of select values in a matrix column, selecting them by their value in a different column. , Give this a try:
code :
X=[1950 1; 1950 2; 1950 3; 1951 1; 1951 5; 1952 1];

years = unique(X(:,1));

for ii=1:length(years)
    yr_index = find(X == years(ii));
    yr_avg(ii) = mean(X(yr_index,2));
end
Keeping the dimension of the matrix but finding values greater than 0.1, and putting them in new matrix

Keeping the dimension of the matrix but finding values greater than 0.1, and putting them in new matrix


By : sandeep sahu
Date : March 29 2020, 07:55 AM
Hope this helps i have a small problem. i have a 3D matrix, and i want to seperate values greater than 0.1 into a new matrix, but keep the dimentions intact.. Because it is 24x45x65. and 24 = hours, 45 = days and 65 = customers. is it possible? i used find() no luck..i used x_weekday(x_weekday > 0.1) i get the values greater than 0.1 but all in 1 column..and it destroys the dimention. , Approach 1:
code :
%%// new_x_weekday would be the one you are after, assuming you want to 
%%// keep the rest of the data that are not greater than 0.1 to be 0

new_x_weekday = zeros(size(x_weekday))
new_x_weekday(x_weekday>0.1) = x_weekday((x_weekday>0.1))
new_x_weekday = x_weekday;
new_x_weekday(new_x_weekday<=0.1)=0
Matlab : how to verify if all values of a matrix are part of a list of values

Matlab : how to verify if all values of a matrix are part of a list of values


By : Erickol Mhumble
Date : March 29 2020, 07:55 AM
wish help you to fix your issue ASantosRibeiro made the best proposal so far (in comments) :
code :
~all(ismember(my_input(:),[0 1]))
Getting a matrix of values from a Matrix with names and list with values

Getting a matrix of values from a Matrix with names and list with values


By : whitejay
Date : March 29 2020, 07:55 AM
this one helps. One way is to unlist L1 and match the names with each element of the matrix
code :
apply(m1, 1:2, function(i) unlist(L1)[match(i, names(L1))])

#      [,1] [,2] [,3] [,4]
#[1,]    1   NA   NA   NA
#[2,]    2   NA    3   NA
#[3,]   NA   NA   NA   NA
Python - finding extracts the same values in a list and calculate the differences between consecutive values

Python - finding extracts the same values in a list and calculate the differences between consecutive values


By : EatYourBeetS VG
Date : March 29 2020, 07:55 AM
this will help I was also working on the same project. I've shared the part of the code you need. I have my .ipynb file of the project on GitHub. You may want to also see my outcome of the function. Cheers!
code :
def read_csv(birth_data_file):
    raw_data = open(birth_data_file, "r").read()
    raw_data = raw_data.split("\n")
    string_list = raw_data[1:]
    final_list = []
    for data in string_list:
        int_fields = []
        string_fields = data.split(",")
        for string_field in string_fields:
            field = int(string_field)
            int_fields.append(field)
        final_list.append(int_fields)
    return(final_list)


def calc_counts(data, column):
    births_counts = {}
    if not column > 0 and column <= 4:
        return("'column' must be either 1, 2, 3, or 4")
    else:
        for instance in data:
            field = instance[column-1]
            births  = instance[4]
            if field in births_counts.keys():
                births_counts[field] += births
            else:
                births_counts[field] = births
        return(births_counts)


# Write a function that extracts the same values across years and calculates the
# differences between consecutive values to show if number of
# births is increasing or decreasing.

def check_birth_growth(birth_data_file):
    cdc_list = read_csv(birth_data_file)
    cdc_year_births = calc_counts(cdc_list, 1)
    previous_year_birth = 0
    previous_birth_diff = 0
    for year, total_births in cdc_year_births.items():
        current_year_birth = int(total_births)
        if previous_year_birth == 0:
            growth_status = "Growth of births in {} not available.".format(year)
            print(growth_status)
            previous_year_birth = current_year_birth
        else:
            if current_year_birth > previous_year_birth:
                growth_status = "Births increased in {}.".format(year)
                print(growth_status)
                previous_year_birth = current_year_birth
            elif current_year_birth < previous_year_birth:
                growth_status = "Births decreased in {}.".format(year)
                print(growth_status)
                previous_year_birth = current_year_birth
            elif current_year_birth == previous_year_birth:
                growth_status = "Births in {} was same as previous year.".format(year)
                print(growth_status)
                previous_year_birth = current_year_birth
Related Posts Related Posts :
  • How to define a function that calls shiny functions?
  • How to count number of observations in a "n" dimensional range in R
  • Superimposing asymmetric t-distribution using ggplot2
  • Makefile to render all targets of all .Rmd files in directory
  • Authentication failure with rdrop2
  • DT data table display error
  • Issue when adding new rows (with nested dataframes within) to a dataframe
  • R-How to compare two dataframe and update list column value
  • Series vector for approximating pi
  • what is difference between "variance explained " in Random Forest and "merror" in XGBoost
  • R - Cast dataframe on unique rows - reshape2
  • ggplot2: plot correct proportions using geom_bar
  • Speedup query for R data.table - can this two-argument function be applied by group more quickly?
  • apply a function to several columns at once with mutate
  • R 'cowplot' neatly produce gridded plot with shared (common) legends and unique legends
  • Repeat R script for many times and save results to text file
  • How to negative lookbehind for special characters
  • data.table inner join produces error when no match is found
  • Create a new column base on existing column, but row above
  • Is there a way to visualize the process of source() in RStudio?
  • google places api consumes 10 request but I am doing only 1
  • Statistical mode of a categorical variable in R (using mlv)
  • Using for-loop to mutate a data.frame in r
  • Make plot with regression line for mixed model
  • Shortcut to select matces cases in R studio
  • vectoriced norm/matrix multiplication
  • Negative log10 transformation in R
  • Plot data with duplicate points
  • Visualizing crosstab tables with a plot in R - changing colours
  • How to manually modify automated numbers and labels in plot
  • How can I follow any redirections of a url in R?
  • Add jitter to box plot using markers in plotly
  • Adding an extra item to the legend
  • ggplot fills in data in the wrong order
  • Convert list to data frame
  • R: filtering by list(s) of strings and returning all results that start with the content of the lists
  • R:How to attach parts of a data frame with different headers and/or an overflowing piece of the dat frame
  • How to use 'par' for manipulating plot margins?
  • Can dplyr::case_when return mix of NAs and non-NAs?
  • Text preprocessing and topic modelling using text2vec package
  • Uploading multiple files in Shiny, process the files, rbind the results and return a download
  • R levelplot: color green-white-red (white on 0) according to one variable, but show the values of another variable
  • Why [i] doesn't point to the starting point in a vector
  • In R after generating a mvrnorm distribution, Y, what does Y[,1] do?
  • expand a data frame to have as many rows as range of two columns in original row
  • Getting started with R and CFA
  • Re order x-axis in ggplot so time goes from 12AM to 11PM in R
  • R - Automatically stack every nth column of a data frame and save them as new objects
  • How to format dplyr output in R into doubles (or other workable format)?
  • Dataframe to matrix conversion using tapply turns zeros to NAs
  • Smallest multiple of 1:20 - How can I make it quicker?
  • How to specify the size of a graph in ggplot2 independent of axis labels
  • How can I find the number of a vector's elements in another vector?
  • ROC curve from train/test set in caret R package
  • Random Forest for a mixture of categorical,numeric and "unwanted" variables which include missing values
  • extract certain data from multiple excel files with R
  • Matrix with counts of wins and losses between methods in R
  • Grouping string variables from a dataframe by best string match to make subsets
  • Reorder does not work after adding second geom_points
  • cover POS data formate to the one can apply Arules (Apriori)
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org