logo
down
shadow

Getting specific key from factor object using doc value in Elasticsearch


Getting specific key from factor object using doc value in Elasticsearch

By : gcbin007
Date : November 22 2020, 10:40 AM
Any of those help The default language of Elasticsearch is groovy and this is not the format to access a map. The following should work for you
code :
{
  "sort": {
    "_script": {
      "script": "factor.get(doc['quality'].value)",
      "type": "number",
      "params": {
        "factor": {
          "high": 2,
          "med": 1,
          "low": 0
        }
      },
      "order": "asc"
    }
  }
}


Share : facebook icon twitter icon
in R sum the number of rows per factor within another factor, and return 0 when 'specific' data is missing

in R sum the number of rows per factor within another factor, and return 0 when 'specific' data is missing


By : OS_sloth
Date : March 29 2020, 07:55 AM
help you fix your problem First method: Using the zoo package as illustrated here very nicely by Dirk. I've explained the code inline. Something like this should do it:
code :
df <- data # I just prefer `df` to `data`
df$storeID <- as.numeric(as.character(df$storeID)) # make sure its numeric
# instantiate the zoo object by providing values corresponding to time
require(zoo)
z <- zoo(as.numeric(as.character(df$storeID)), 
              as.POSIXct(paste(df$Date, df$Time)))
# create output data.frame with all possible timings
open_time <- paste(9:18, "00", "00", sep=":")
open_date <- as.character(unique(df$Date))
out.df <- data.frame(Date = rep(open_date, each=length(open_time)-1), 
                     Start = rep(head(open_time, -1), length(open_date)), 
                     End = rep(tail(open_time, -1), length(open_date)))
# Pointer for matching later
out.df$Pointer <- as.POSIXct(paste(out.df$Date, out.df$Start))
# initialise count to 0
out.df$count <- 0

# aggregate using zoo's magic function!
# the first part contains the storeID and is aggregated by
# the second column which creates hourly interval from the times in z (your data)
# and the third column sums up all values that fall in each hourly interval
agg.out <- aggregate(z, time(z) - as.numeric(time(z)) %% 3600, length)

# once that is done, just match the corresponding times and place them rightly
m.idx <- match( out.df$Pointer, index(agg.out))
out.df$count[!is.na(m.idx)] <- agg.out[m.idx[!is.na(m.idx)]]
out.df <- subset(out.df, select=-c(Pointer))

# and you're done
> out.df
#          Date    Start      End count
# 1  2012-11-01  9:00:00 10:00:00     0
# 2  2012-11-01 10:00:00 11:00:00     0
# 3  2012-11-01 11:00:00 12:00:00     0
# 4  2012-11-01 12:00:00 13:00:00     3
# 5  2012-11-01 13:00:00 14:00:00     0
# 6  2012-11-01 14:00:00 15:00:00     1
# 7  2012-11-01 15:00:00 16:00:00     0
# 8  2012-11-01 16:00:00 17:00:00     1
# 9  2012-11-01 17:00:00 18:00:00     0
# 10 2012-11-02  9:00:00 10:00:00     0
# 11 2012-11-02 10:00:00 11:00:00     0
# 12 2012-11-02 11:00:00 12:00:00     0
# 13 2012-11-02 12:00:00 13:00:00     5
# 14 2012-11-02 13:00:00 14:00:00     4
# 15 2012-11-02 14:00:00 15:00:00     4
# 16 2012-11-02 15:00:00 16:00:00     1
# 17 2012-11-02 16:00:00 17:00:00     0
# 18 2012-11-02 17:00:00 18:00:00     1
require(data.table)
df <- data # I prefer df than data
# create an id column containing only the hours
df$id <- as.numeric(as.POSIXlt(paste(df$Date, df$Time))$hour)
# convert Date to character
df$Date <- as.character(df$Date)

# load package, create input data.table with Date and id as keys
require(data.table)
dt.in <- data.table(df)
setkey(dt.in, "Date", "id")
# get the count of customers / hour / date 
dt.tmp <- dt.in[, .N, by=c("Date", "id")]

# create the output template data.table with Date, Start and End
open_time <- paste(9:18, "00", "00", sep=":")
open_date <- as.character(unique(df$Date))
dt.out <- data.table(Date = rep(open_date, each=length(open_time)-1), 
                     Start = rep(head(open_time, -1), length(open_date)), 
                     End = rep(tail(open_time, -1), length(open_date)))
# create the id again by extracting hour                     
dt.out[, id := as.numeric(as.POSIXlt(paste(Date, Start))$hour)]
setkey(dt.out, "Date", "id")

# merge the two data.tables to get your output
dt.out <- dt.tmp[dt.out, list(Start, End, N)]
dt.out[, id := NULL]

> dt.out
#           Date    Start      End  N
#  1: 2012-11-01  9:00:00 10:00:00 NA
#  2: 2012-11-01 10:00:00 11:00:00 NA
#  3: 2012-11-01 11:00:00 12:00:00 NA
#  4: 2012-11-01 12:00:00 13:00:00  3
#  5: 2012-11-01 13:00:00 14:00:00 NA
#  6: 2012-11-01 14:00:00 15:00:00  1
#  7: 2012-11-01 15:00:00 16:00:00 NA
#  8: 2012-11-01 16:00:00 17:00:00  1
#  9: 2012-11-01 17:00:00 18:00:00 NA
# 10: 2012-11-02  9:00:00 10:00:00 NA
# 11: 2012-11-02 10:00:00 11:00:00 NA
# 12: 2012-11-02 11:00:00 12:00:00 NA
# 13: 2012-11-02 12:00:00 13:00:00  5
# 14: 2012-11-02 13:00:00 14:00:00  4
# 15: 2012-11-02 14:00:00 15:00:00  4
# 16: 2012-11-02 15:00:00 16:00:00  1
# 17: 2012-11-02 16:00:00 17:00:00 NA
# 18: 2012-11-02 17:00:00 18:00:00  1
How do I make a specific factor in a vector have a higher level than every other factor?

How do I make a specific factor in a vector have a higher level than every other factor?


By : Cheryl
Date : March 29 2020, 07:55 AM
seems to work fine Given a vector for which "b" will always be an element of, how do I make "b" have a higher level than all the other factors (without reordering the other factors relative to each other)?
code :
# Make some data
x <- c("a", "b", "c", "b", "a")
x <- factor(x)
> x
[1] a b c b a
Levels: a b c
# save the current levels
lev <- levels(x)
# Find the one we want
# So change this to specify the level you want to be last
val <- which(lev == "b")
# Remake the factor specifying the order we want
x <- factor(x, levels = c(lev[-val], lev[val]))

> x
[1] a b c b a
Levels: a c b
Removing Specific factor level from factor variable

Removing Specific factor level from factor variable


By : Lamartine Cabral
Date : March 29 2020, 07:55 AM
should help you out I have a data frame that has several variables that have 5 factor levels. I want to delete only one of those levels. First I assigned all instances of of that level to NA, and then used the droplevels command to get rid the empty levels.
code :
str(
  as.data.frame(
    lapply(
      df2, 
      function(x) factor(as.character(x), levels=levels(x)[levels(x) != "e"])
) ) )
# 'data.frame':  10 obs. of  3 variables:
# $ var1: Factor w/ 4 levels "a","b","c","d": 1 2 3 4 NA 1 2 3 4 NA
# $ var2: Factor w/ 4 levels "a","b","c","d": NA 4 3 2 1 NA 4 3 2 1
# $ var3: Factor w/ 4 levels "a","c","d","b": 1 2 3 NA 1 2 3 NA 1 2
In R: replacing specific factor in a dataframe with another existing factor

In R: replacing specific factor in a dataframe with another existing factor


By : Miguel García
Date : March 29 2020, 07:55 AM
This might help you I have a large data frame with a variable 'home_ownership' which can take the factors : 'own', 'mortgage', 'none', 'other' , there's a typo in
code :
df$home_ownership[df$home_owneship == "none"] <- "other"
levels(df$home_ownership)[levels(df$home_ownership)=="none"] <- "other"
How to remove one specific factor level in all factor variables in r?

How to remove one specific factor level in all factor variables in r?


By : ankushsoni
Date : March 29 2020, 07:55 AM
should help you out For example, I have data frame with 6 columns (all are factors). , I think this should accomplish what you are trying to do.
code :
dfNew <- data.frame(lapply(df, function(x) {is.na(x[x=="no"]) <- TRUE; droplevels(x)}))
set.seed(1234)
df <- data.frame(q1=sample(c("yes", "no", "maybe"), 20, replace=TRUE),
                 q2=sample(c("yes", "no", "maybe"), 20, replace=TRUE),
                 q3=sample(c("yes", "no", "maybe"), 20, replace=TRUE))
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org