C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD

# 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 :

## 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]

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

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

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

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>
``````