logo
down
shadow

Finding the first non-zero year in data frame for multiple variables using tidyverse


Finding the first non-zero year in data frame for multiple variables using tidyverse

By : Satish Thota
Date : October 19 2020, 08:10 AM
hope this fix your issue I have the following data: , We may do the following:
code :
test %>% group_by(id) %>% summarise_at(vars(var1:var2), funs(Year[. > 0][1]))
# A tibble: 3 x 3
#      id  var1  var2
#   <dbl> <dbl> <dbl>
# 1     1    NA  2001
# 2     2  2002  2003
# 3     3  2001  2001


Share : facebook icon twitter icon
finding the max of a series of variables in a data-frame

finding the max of a series of variables in a data-frame


By : JUDE
Date : March 29 2020, 07:55 AM
I wish this help you If it is the max elements, then we can use max.col. Create a logical index of the numeric column names using grepl ('i1'), then subset the dataset based 'i1' (df1[i1]) get the index of the max value for each row with max.col and use that to get the corresponding column names.
code :
i1 <- grepl("[0-9]+$", names(df1))
df1$newVar <- names(df1)[i1][max.col(df1[i1], "first")]
df1$newVar
#[1] "2011" "2013"
names(df1)[i1][apply(df1[i1], 1, which.max)]
#[1] "2011" "2013"
df1 <- structure(list(id = c(268672L, 351003L), cluster = c("Type 1", 
"Type 2"), username = c("Vlaam", "WikiCleanerBot"), `2001` = c(0L, 
0L), `2002` = c(0L, 0L), `2003` = c(0L, 0L), `2004` = c(0L, 0L
), `2005` = c(0L, 0L), `2006` = c(0L, 0L), `2007` = c(5896L, 
0L), `2008` = c(18976L, 17049L), `2009` = c(13552L, 8468L), 
`2010` = c(20508L, 
22834L), `2011` = c(106939L, 7470L), `2012` = c(83874L, 11219L
), `2013` = c(97447L, 83245L), `2014` = c(59677L, 28015L), 
`2015` = c(88661L, 
40464L), `2016` = c(41133L, 25053L)), .Names = c("id", "cluster", 
 "username", "2001", "2002", "2003", "2004", "2005", "2006", "2007", 
 "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015", 
 "2016"), row.names = c("1", "2"), class = "data.frame")
Pandas Data frame Finding data related to rolling one year

Pandas Data frame Finding data related to rolling one year


By : amani
Date : March 29 2020, 07:55 AM
Regressing a data frame of multiple dependent variables on a data frame of multiple explanatory variables

Regressing a data frame of multiple dependent variables on a data frame of multiple explanatory variables


By : Vanz randell Paraan
Date : March 29 2020, 07:55 AM
around this issue Your first attempt was great except that you should have provided matrices rather than data frames:
code :
lm(as.matrix(dependents) ~ as.matrix(explanatory))
lm(as.matrix(dependents) ~ -1 + model.matrix(~ ., data = explanatory))
lapply(dependents, function(y) lm(y ~ ., data = cbind(y = y, explanatory)))
tidyverse: Cross tables of one variable with all other variables in data.frame

tidyverse: Cross tables of one variable with all other variables in data.frame


By : Meh Di
Date : March 29 2020, 07:55 AM
Does that help tably takes names as arguments and you passed a vector to it.
If you use imap you'll have access to the name of the column, that you can convert to a symbol, and as janitor supports quasi-quotation you can write:
code :
humans %>%
  select_if(is.character) %>%
  select(-name, -gender) %>%
  imap(.f = ~janitor::tabyl(dat = humans, !!sym(.y), gender))
#$`hair_color`
#     hair_color female male
#         auburn      1    0
#   auburn, grey      0    1
#  auburn, white      0    1
#          black      1    7
#          blond      0    3
#          brown      6    8
#    brown, grey      0    1
#           grey      0    1
#           none      0    3
#          white      1    1
# 
# $skin_color
#  skin_color female male
#        dark      0    4
#        fair      3   13
cols <- setdiff(names(Filter(is.character,humans)), c("name","gender"))
lapply(cols, function(x) janitor:::tabyl_2way(humans, as.name(x), quote(gender)))
# [[1]]
#     hair_color female male
#         auburn      1    0
#   auburn, grey      0    1
#  auburn, white      0    1
#          black      1    7
#          blond      0    3
#          brown      6    8
#    brown, grey      0    1
#           grey      0    1
#           none      0    3
#          white      1    1
# 
# [[2]]
#  skin_color female male
#        dark      0    4
humans %>%
  select_if(is.character) %>%
  select(-name, -gender) %>%
  imap(.f = ~tabyl(dat = humans, !!sym(.y), gender) %>% rename_at(1,~"x")) %>%
  xtableList
cols <- setdiff(names(Filter(is.character,humans)), c("name","gender"))
l <- lapply(cols, function(x) {
  res <- janitor:::tabyl_2way(humans, as.name(x), quote(gender))
  names(res)[1] <- "x"
  res
})
xtableList(l)
Is there any way to extract year and month from date data in R by tidyverse or anything?

Is there any way to extract year and month from date data in R by tidyverse or anything?


By : shhada
Date : March 29 2020, 07:55 AM
around this issue If the data.frame contains 20100101, 2010-01-01, 2010/01/01, three types style time data, how can I get the column like "%Y-%m" or Jan 2010. , As they all follow year, month, day, we can use ymd
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