logo
down
shadow

R :Looping through each 5 rows of data frame and imputing incremental value


R :Looping through each 5 rows of data frame and imputing incremental value

By : ahmed gamal
Date : October 21 2020, 08:10 PM
will be helpful for those in need I am trying to impute incremental values for each 5 rows of the data frame. I am new to R and not sure how to achieve this. , This is your data:
code :
df = read.table(text = 
                "state Value 
                     a     1
                     b     2
                     a     3
                     c     4
                     a     5
                     e     6
                     f     7
                     w     8
                     f     9
                     s     10
                     e     11
                     r     12
                     s     13
                     s     14", 
                header=T)
df$Increment <- ceiling(as.numeric(rownames(df))/5)
#    state Value Increment
# 1      a     1         1
# 2      b     2         1
# 3      a     3         1
# 4      c     4         1
# 5      a     5         1
# 6      e     6         2
# 7      f     7         2
# 8      w     8         2
# 9      f     9         2
# 10     s    10         2
# 11     e    11         3
# 12     r    12         3
# 13     s    13         3
# 14     s    14         3


Share : facebook icon twitter icon
Imputing Missing Values in R from reference data frame

Imputing Missing Values in R from reference data frame


By : Ugur Celikkiran
Date : March 29 2020, 07:55 AM
wish helps you I have a data frame 'dat' of dim 17000 x 3 of walking data. The interval column is 5 minute intervals for each 24 hour period, the date column is the date and the steps column is the number of steps taken in said 5 minute period on said date. NA's are present. , This is a little clunky, but it works:
code :
library(dplyr)
df1.1 <- df1 %>%
    group_by(date) %>%
    summarise(avg = mean(steps, na.rm = TRUE)) %>%
    merge(df1, ., all.x=TRUE) %>%
    mutate(steps = ifelse(is.na(steps)==TRUE, avg, steps)) %>%
    select(-avg)
df1 <- data.frame(date = c(rep("2015-01-01", 12), rep("2015-01-02", 12)), interval = rep(seq(12), 2),
    steps = c(5, 7, NA, 12, 3, NA, 0, 4, 12, 10, 4, 0, 3, NA, 2, 1, NA, 15, 0, 4, 7, 2, NA, 2),
    stringsAsFactors = FALSE)
> head(df1)
        date interval steps
1 2015-01-01        1     5
2 2015-01-01        2     7
3 2015-01-01        3    NA
4 2015-01-01        4    12
5 2015-01-01        5     3
6 2015-01-01        6    NA 
> head(df1.1)
        date interval steps
1 2015-01-01        1   5.0
2 2015-01-01        2   7.0
3 2015-01-01        3   5.7
4 2015-01-01        4  12.0
5 2015-01-01        5   3.0
6 2015-01-01        6   5.7
> df1 %>% group_by(date) %>% summarise(avg = mean(steps, na.rm = TRUE))
Source: local data frame [2 x 2]

        date avg
1 2015-01-01 5.7
2 2015-01-02 4.0
Creating data frame with incremental minutes as rows (R)

Creating data frame with incremental minutes as rows (R)


By : Melawati
Date : March 29 2020, 07:55 AM
it helps some times Look up the function seq.POSIXt. This function is designed to create sequences of time. For your problem:
code :
seq(ISOdate(2016,2,02, 14, 00, 00), by = "min", length.out = 5)
Imputing values in all columns of data.frame with mice

Imputing values in all columns of data.frame with mice


By : Shawn Debnath
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I am trying to impute values using a linear model using mice. My understanding of mice is that it iterates over the rows. For a column with NAs it is using all other columns as predictors, fits the model, and then samples from this model to fill up the NAs. Here is an example where I generate some data, and than introduce missing data using ampute. , I tried to run your code and end up with the same type of problem:
code :
library(mice)
n <- 100
xx<-data.frame(x = 1:n + rnorm(n,0,0.1), y =(1:n)*2 + rnorm(n,0,1))
head(xx)
res <- (ampute(xx))
head(res$amp)
tempData <- mice(res$amp,m=5,maxit=50,seed=500)
summary(tempData)
Multiply imputed data set
Call:
mice(data = res$amp, m = 5, maxit = 50, seed = 500)
Number of multiple imputations:  5
Missing cells per column:
 x  y 
21 23 
Imputation methods:
    x     y 
"pmm" "pmm" 
VisitSequence:
x 
1 
PredictorMatrix:
   x  y
x  0  0
y  0  0
Random generator seed value:  500 
n <- 100
xx<-data.frame(x = 1:n + rnorm(n,0,0.1), y =(1:n)**2 + rnorm(n,0,1))
head(xx)
res <- (ampute(xx))
head(res$amp)
Pandas: Imputing Missing Values to Data Frame

Pandas: Imputing Missing Values to Data Frame


By : Timothy Smith
Date : March 29 2020, 07:55 AM
it helps some times In pandas NA should be NaN, 1st you need to replace it , then we can using fillna
code :
df.Y=df.Y.replace('NA',np.nan)
df.Y=df.Y.fillna(pd.Series([1,2],index=df.index[df.Y.isnull()]))
df
Out[1375]: 
   W  X    Y  Z
0  1  3  1.0  2
1  0  1  1.0  3
2  1  2  2.0  1
df.loc[df.Y=='NA','Y']=[1,2]
df
Out[1380]: 
   W  X  Y  Z
0  1  3  1  2
1  0  1  1  3
2  1  2  2  1
Looping over rows of a data frame to simulate

Looping over rows of a data frame to simulate


By : jzsn
Date : March 29 2020, 07:55 AM
wish of those help This is more of a programing in R question than any concept question. I tried but my lack of expertise in R is frustrating me: , The random draw functions are all vectorized:
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