logo
down
shadow

Merging two incomplete factors


Merging two incomplete factors

Content Index :

Merging two incomplete factors
Tag : r , By : Michael Gunderson
Date : November 26 2020, 04:01 AM

this will help I find myself in the following situation: , We could replace it with NA and then use coalesce
dt$ab <- do.call(dplyr::coalesce, replace(dt[-1], dt[-1] == 'unknown', NA))
do.call(pmax, c(replace(dt[-1], dt[-1] == 'unknown', NA), list(na.rm = TRUE)))
#[1] "sch" "con" "sim" "sch" "con" "con"
dt <- data.frame(ID, a, b, stringsAsFactors = FALSE)

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Merging of 2 factors in R with large no. of levels


Tag : r , By : smbrant
Date : March 29 2020, 07:55 AM
This might help you I have a plain text format file data_table_complete of size of 13 GB with over 100 columns in which 1 there is a column related to color. , So lets say this is your data frame
df <- data.frame(color = factor(c(rep("red",4), rep("OTHERS", 4),rep("blue", 5), rep("OTHETRS",5))))
table(df$color)
#blue  OTHERS OTHETRS     red 
#   5       4       5       4 
df$color <- factor(ifelse(df$color == "OTHERS" | df$color == "OTHETRS", "OTHETRS", as.character(df$color)))
table(df$color)
#blue OTHETRS     red 
#   5       9       4 

Merging multiple rows with multiple factors to create a new row in a dataset


Tag : r , By : Ravenal
Date : March 29 2020, 07:55 AM
I wish this help you My answer uses aggregate() and does not require any other package.
Replace df by your data frame name.
df$VegType <- factor(df$VegType)
levels(df$VegType) <- list(WoodyVeg=c("Shrub", "Sapling", "Vine"), Forb=c("Forb"),Grass=c("Grass"))
df1<-aggregate(df[,4:13],by=list(df$TranID,df$PT,df$VegType),FUN=sum)
names(df1)<-names(df)
df1[with(df1, order(df1$PT)),]
   TranID PT  VegType Int1 Int2 Int3 Int4 Int5 Int6 Int7 Int8 Int9 Int10
       1 1M WoodyVeg    1    0    2    1    6    9    0    0    5     0
       1 1M     Forb    0    1    0    0    0    0    0    0    0     0
       1 1M    Grass    1    1    1    0    0    0    0    0    0     0
       1 2M WoodyVeg    1    0    2    1    6    9    0    0    5     0
       1 2M     Forb    0    1    0    0    0    0    0    0    0     0
       1 2M    Grass    1    1    1    0    0    0    0    0    0     0
       1 3M WoodyVeg    1    0    2    1    6    9    0    0    5     0
       1 3M     Forb    0    1    0    0    0    0    0    0    0     0
       1 3M    Grass    1    1    1    0    0    0    0    0    0     0
       1 4M WoodyVeg    1    0    2    1    6    9    0    0    5     0
       1 4M     Forb    0    1    0    0    0    0    0    0    0     0
       1 4M    Grass    1    1    1    0    0    0    0    0    0     0
       1 5M WoodyVeg    1    0    2    1    6    9    0    0    5     0
       1 5M     Forb    0    1    0    0    0    0    0    0    0     0
       1 5M    Grass    1    1    1    0    0    0    0    0    0     0

Recalculate the new weighted mean when merging two factors by group, and keep original data


Tag : r , By : AdrianB
Date : March 29 2020, 07:55 AM
this will help got to admit it was challenging...you should reconsider the data structure
library(tidyverse)

set.seed(123)
df <- data.frame(ID = 1:20, 
                 total_X = runif(20), 
                 min_X = runif(20),
                 max_X = runif(20),
                 mean_X = runif(20),
                 total_Y = runif(20), 
                 min_Y = runif(20),
                 max_Y = runif(20),
                 mean_Y = runif(20),
                 Counts = runif(20)*1000,
                 category = rep(letters[1:5], 4), 
                 file = as.factor(sort(rep(1:4, 5)))) 


x <- df %>% bind_rows(
  gather(df,metric,value,-ID,-file,-category,-Counts) %>% 
    mutate(group=str_extract(metric,"[A-Z]$"),metric = str_replace(metric,"_.$","")) %>% 
    filter(category %in% c('a' , 'b')) %>% 
    spread(metric,value) %>% 
    group_by(file,group) %>% 
    summarise(Counts = mean(Counts),
              category = paste0(category,collapse = ''),
              max = max(max),
              min = min(min),
              total = sum(total),
              mean = sum(Counts * mean)/sum(Counts)) %>% 
    ungroup() %>% 
    gather(metric,value,-file,-group,-category,-Counts) %>% 
    mutate(metric = paste(metric,group,sep='_'),group=NULL) %>% 
    spread(metric,value) %>% 
    mutate(ID=0)
) %>% mutate(ID = row_number())

merging incomplete duplicate rows


Tag : r , By : orneka
Date : March 29 2020, 07:55 AM
I hope this helps . If we want to sample a row after grouping by 'dates', 'co.name', we can use that in slice
library(dplyr)
df %>%
   group_by(dates, co.name) %>% 
   slice(sample(row_number(), 1))
df %>% 
   group_by(dates, co.name) %>% 
   sample_n(1)

merging data frames while assigning factors to missing data


Tag : r , By : brennen
Date : March 29 2020, 07:55 AM
Related Posts Related QUESTIONS :
  • How to retrieve the data frame used in a GEE model fit?
  • R: How to find/replace and then automatically execute code?
  • Slope of time series (xts) object over rolling window
  • Is there an R function for comparing rows in data.frame?
  • Changing linetype and line color with plot_model()
  • Update existing package on CRAN
  • Delete NA data ,but with certain condition in R
  • calculate number and names of similar sounding words from a data frame
  • Reset input fields of dynamically generated widgets through insertUI
  • How to get the coordinates that spits out min/max value from the function in R?
  • running t.test() on multiple columns to output tibble
  • Conditionally replace the values in columns to value in another column using dplyr
  • Add count as label to points in geom_count
  • Temporarily Disable Rprofile
  • Select certain region of column for lm
  • Convert multiple rows into one row depending on unique values in another column
  • Issues installing Plotly Dash for R
  • Is there an R function to retrieve values from a matrix of column names?
  • R;Too slow to overate loops for million vectors
  • How to optimize intersect of rows and columns in a matrix?
  • Format and export the output of Mann-Kendall test in R to excel from Rstudio
  • how to calculate cumsum with depreciation in a grouped dataframe?
  • reshape wide to long based on part of column name
  • How to get a hyperlink for the words in a description in an r dataframe?
  • shinymeta works locally but breaks when published to shinyapps.io
  • Deparse and (un)escape quotes
  • Regression table with clustered standard errors in R jupyter notebook?
  • Disaggregate quarterly data to daily data in R keeping values?
  • How to save output to console and file simultaneously in RStudio server?
  • Why does data.table j have a different environment when directly calling mget() vs calling mget() inside another functio
  • scale_fill_viridis_c color bar on a log scale
  • How to change the lab name corresponding to function in ggplot
  • R, filtering for an element in a list in a dataframe cell
  • Extracting only bottom temperature from 4d NetCDF file
  • How to add/wrap lines of text to .tex with .sh script
  • R - building new variables from sequenced data
  • Sum rows values one after the other
  • Nesting ifelse inside summarytools
  • How best to divide different levels of a factor by one another in dataframe in R?
  • Why does my code run multiple times before I type data into the table? How do I make an action button that creates a tab
  • How to impute missing values not at random?
  • Set the y limits of an added average line of a plotly plot
  • how to calculate a new column after grouping with dplyr
  • Extract data from rows creating new columns using R
  • Create a filled area line plot with plotly
  • When do I need parentheses around an if statement to control the sequence of a formula in R?
  • my graph in ggplot2 contains an "e" character in y-axis
  • Making variables immutable in R
  • R: Difference between the subsequent ranks of a item group by date
  • Match data within multiple time-frames with dplyr
  • Conditional manipulation and extension of rows in data.table also considering previous extensions without for-loop
  • Conditional formula referring to preview row in DF not working
  • Set hoverinfo text in plotly scatterplot
  • Histogram of Sums from Categorical/Binary Data
  • Efficiently find set differences and generate random sample
  • Find closest points from data set B to point in data set A, using lat long in R
  • dplyr join on column A OR column B
  • Replace all string if row starts with (within a column)
  • Is there a possibility to combine position_stack and nudge_x in a stacked bar chart in ggplot2?
  • How can I extract bounding boxes in a row-wise manner using R?
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com