logo
down
shadow

R data.table to calculate a formula using a column as a variable across levels of a factor


R data.table to calculate a formula using a column as a variable across levels of a factor

By : user2952633
Date : November 19 2020, 01:01 AM
should help you out I want to calculate the output sum_logloss (see below) across all levels of a factor (C1) using a data table formula. But the result is not what I expect. Here is a small example showing what I get and why I expect a different sum_logloss as outcome. , You could try
code :
 df[, paste0('V', 1:2):=list(mean(click), .N), by=C1][,
    list(mean_CTR=V1[1L], count=V2[1L], sum_logloss=
              TotalLogLossVector(click, V1)), by=C1]

 #  C1  mean_CTR count sum_logloss
 #1:  1 0.6666667     3    1.909543
 #2:  2 0.5000000     2    1.386294


Share : facebook icon twitter icon
How does one change the levels of a factor column in a data.table

How does one change the levels of a factor column in a data.table


By : Will Geller
Date : March 29 2020, 07:55 AM
I wish this helpful for you What is the correct way to change the levels of a factor column in a data.table (note: not data frame) , You can still set them the traditional way:
code :
levels(mydt$value) <- c(...)
setattr(mydt$value,"levels",c(...))
Return all factor levels by name as new columns from a three column data.table [R]

Return all factor levels by name as new columns from a three column data.table [R]


By : HansDeKling
Date : March 29 2020, 07:55 AM
hop of those help? This is similar to creating dummy variables.
code :
uc <- sort(unique(as.character(DT$c)))
DT[,(uc):=lapply(uc,function(x)ifelse(c==x,b,NA))][,c('b','c'):=NULL]
uc <- sort(unique(as.character(DT$c)))
is <- 1:nrow(DT)
js <- as.character(DT$c)
vs <- DT$b

DT[,(uc):=NA_integer_]
for (i in is) set(DT,i=is[i],j=js[i],value=vs[i])

DT[,c('b','c'):=NULL]
How to get distinct values between different levels of a factor variable in a data.table

How to get distinct values between different levels of a factor variable in a data.table


By : Patxmay
Date : March 29 2020, 07:55 AM
should help you out We can do this by grouping with 'name' and if the length of 'unique` 'time' elements is 1, then get the 'time'
code :
dt[, if(uniqueN(time)==1) .(time = time) , name]
#     name   time
#1: Charlie  lunch
#2:  Dennis dinner
library(dplyr)
dt %>%
    group_by(name) %>%
    filter(n_distinct(time)==1) %>%
    select(-meal)
Calculate Column Percentages within factor levels

Calculate Column Percentages within factor levels


By : Mr.Matin
Date : March 29 2020, 07:55 AM
With these it helps I am trying to find the column percents for each level (Level_1 and Level_2), within each row (V1, V2, V3, V4). , You can also use this:
code :
lapply(split(DF[, -1], DF$F1), function(x) t(t(x) / colSums(x)))

$Level_1
         V1   V2        V3        V4
1 0.1052632 0.50 0.4090909 0.4736842
2 0.3684211 0.15 0.2727273 0.3684211
3 0.5263158 0.35 0.3181818 0.1578947

$Level_2
   V1        V2        V3        V4
4 0.5 0.2727273 0.3333333 0.2307692
5 0.4 0.2727273 0.1666667 0.4615385
6 0.1 0.4545455 0.5000000 0.3076923
data.table::melt - variable column converted to factor with variable.factor = FALSE specified

data.table::melt - variable column converted to factor with variable.factor = FALSE specified


By : INdaba Ndaba
Date : March 29 2020, 07:55 AM
Hope this helps The reason this is happening, is because df is a dataframe. In such cases melt from falls back to the behavior of melt from which doesn't have a variable.factor argument.
You can see this in the source coude of data.table::melt:
code :
> data.table::melt
function (data, ..., na.rm = FALSE, value.name = "value") 
{
    if (is.data.table(data)) 
        UseMethod("melt", data)
    else reshape2::melt(data, ..., na.rm = na.rm, value.name = value.name)
}
<bytecode: 0x10f886b88>
<environment: namespace:data.table>
melt(
  df,
  id.vars = c("bloc", "name"),
  variable.name = "time",
  value.name = "severity",
  variable.factor = FALSE
) %>% str()
'data.frame': 132 obs. of  4 variables:
 $ bloc    : int  1 1 2 2 3 3 4 4 5 5 ...
 $ name    : chr  "Cristina" "Robijn" "Robijn" "Cristina" ...
 $ time    : Factor w/ 11 levels "d1","d7","d10",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ severity: num  0 0 0 0 0 0 0 0 0 0 ...
melt(
  setDT(df),
  id.vars = c("bloc", "name"),
  variable.name = "time",
  value.name = "severity",
  variable.factor = FALSE
) %>% str()
Classes ‘data.table’ and 'data.frame':    132 obs. of  4 variables:
 $ bloc    : int  1 1 2 2 3 3 4 4 5 5 ...
 $ name    : chr  "Cristina" "Robijn" "Robijn" "Cristina" ...
 $ time    : chr  "d1" "d1" "d1" "d1" ...
 $ severity: num  0 0 0 0 0 0 0 0 0 0 ...
 - attr(*, ".internal.selfref")=<externalptr>
Related Posts Related Posts :
  • Using German characters (ü, ö, ä, etc.) in text analysis (R)
  • R JAGS: Dimension mismatch
  • R - How to create a function that accepts a code block as parameter?
  • How do I manually set geom_bar fill color in ggplot
  • How can I get my points to connect in a plot and show a trend with NA values in data?
  • Read SPecific lines of a CSV file in R-language
  • ggplot stacked bar plot from 2 separate data frames
  • auto.arima not parallelizing
  • Histogram of binned data frame in R
  • R rewriting stringmanipulations implemented in loop to the R-way
  • get first entries in rows of list?
  • Conditionally removing rows from a matrix in R
  • Using a loop to find P(-1.5<Y<1.5) for a range of sample sizes
  • R-Count and list the maximum count row by row
  • Include Iverson Bracket in R documentation
  • update a data frame and environment in R
  • How to write dynamic cumulative multiple in R
  • format time using as.POSIX in R
  • Change the class of multiple columns
  • Remove period and spaces within column headings nested in a list of data frames
  • R: error message --- package error: "functionName" not resolved from current namespace
  • labels with geom_text ggplot2
  • Passing mongodb ISODate in R
  • Importing "csv" file with multiple-character separator to R?
  • Change row names of a table obtained from a lm regression using xtable function
  • R language iterate over R object
  • How do you delete the header in a dataframe?
  • Re coding in R using complicated statement
  • accumulating functions and closures in R
  • How do you combine two columns into a new column in a dataframe made of two or more different csv files?
  • Twitter authentication fails
  • Summing Values of One Vector Conditional on Values of Another Vector
  • draw cube into 3D scatterplot in RGL
  • lme4 translate formula to code in 3-level model
  • How to draw single axis plot in R
  • Combine geom_tile() and facet_grid/facet_wrap and remove space between tiles (ggplot2)
  • Use snpStats with R version 3.0.1
  • Makefile gives strange error while compiling markdown file into .docx file
  • How to determine whether a points lies in an ellipse
  • Summarize data already grouped in r
  • Is the bigvis package for R not available for R version 3.0.1?
  • Operator overloading in R reference classes
  • How to enable user to switch between ggplot2 and gVis graphs in R Shiny?
  • Is there an easy way to separate categorical vs continuous variables into two dataset in R
  • Correct previous year by id within R
  • Installation of rdyncall package for R
  • ggplot2 plot that evaluates the percentage and mean of a third variable at intersecting points
  • Error Handling with Lapply
  • data.table - split multiple columns
  • How to compute the overall mean for several files in R?
  • R: Graph Plotting: Subscripts in the legend like LaTeX
  • Restructuring data in R
  • Distance of pointsfrom cluster centers after K means clustering
  • R incorrect value of date function
  • Package "Imports" not loading in R development package
  • r - run a user defined function several times by taking column elements as parameters
  • Create input$selection to subset data AND radiobuttons to choose plot type in Shiny
  • Generate crosstabulations from dataframe of categorical variables in survey
  • Restructure output of R summary function
  • New behavior in data.table? .N / something with `by` (calculate proportion)
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org