logo
down
shadow

How to assign ID to multiple rows based on a value in 1 column in 1 row duplicating a value in a DIFFERENT column in a d


How to assign ID to multiple rows based on a value in 1 column in 1 row duplicating a value in a DIFFERENT column in a d

Content Index :

How to assign ID to multiple rows based on a value in 1 column in 1 row duplicating a value in a DIFFERENT column in a d
Tag : r , By : Francesco
Date : November 29 2020, 04:01 AM

Hope this helps When a call is placed to an emergency line, it is given a CallNo (a unique to the event); however, sometimes, multiple calls are placed and different call takers accidentally assign them different call numbers. Later, the CallNo of the other call (the DupCallNo) is appended on to EACH call. , How about creating a unique ID from the two columns:
library(tidyverse)

df %>% rowwise() %>%  
  mutate(Combined = paste0(min(CallNo, DupCallNo, na.rm = TRUE), max(CallNo,DupCallNo, na.rm = TRUE))) 

# A tibble: 4 x 5
# Groups:   Combined [2]
  CallNo DupCallNo Priority Unit    Combined
   <int>     <int> <fct>    <fct>   <chr>   
1    123       255 A        Bravo12 123255  
2    255       123 A        Bravo44 123255  
3    366       476 B        Xray22  366476  
4    476       366 A        Xray109 366476 

Comments
No Comments Right Now !

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

Share : facebook icon twitter icon

Duplicating rows based on a column value in each row


Tag : sql-server , By : ArdentRogue
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further This will support over 7,400 repeats for any individual job (on my system). If you need more, you can use a different system table or a cross join.
DECLARE @d TABLE (Job INT, Quantity INT, Status VARCHAR(12), Repeat INT);

INSERT @d SELECT 1, 100, 'OK'  ,2
UNION ALL SELECT 2, 400, 'HOLD',0
UNION ALL SELECT 3, 200, 'HOLD',1
UNION ALL SELECT 4, 450, 'OK'  ,3;

WITH x AS 
(
  SELECT TOP (SELECT MAX(Repeat)+1 FROM @d) rn = ROW_NUMBER() 
  OVER (ORDER BY [object_id]) 
  FROM sys.all_columns 
  ORDER BY [object_id]
)
SELECT * FROM x
CROSS JOIN @d AS d
WHERE x.rn <= d.Repeat + 1
ORDER BY Job;

Splitting (duplicating rows of) an SQL table based on the values in one column


Tag : sql-server , By : beebob
Date : March 29 2020, 07:55 AM
Hope that helps I have a table which is the result of an export and has the columns and values like the following
with cte as (
    select Name, cast(null as int) as AliasStartPosition, cast(0 as int) as AliasEndPosition, Aliases + ' ' as Aliases, cast(null as int) as RankStartPosition, cast(0 as int) as RankEndPosition, Ranks + ' ' as Ranks
    from (
        values ('Ben', 'BenA BenB BenC', '1 5 3'),
               ('Jerry', 'JerryA JerryB', '7 3')
    ) t (Name, Aliases, Ranks)

    union all

    select Name, AliasEndPosition + 1, charindex(' ', Aliases, AliasEndPosition + 1), Aliases, RankEndPosition + 1, charindex(' ', Ranks, RankEndPosition + 1), Ranks
    from cte
    where charindex(' ', Aliases, AliasEndPosition + 1) != 0
)
select Name, substring(Aliases, AliasStartPosition, AliasEndPosition - AliasStartPosition) as Alias, substring(Ranks, RankStartPosition, RankEndPosition - RankStartPosition) as Rank 
from cte
where AliasStartPosition is not null

Excel - How to find strings in Column B based on multiple rows in Column A and return the corresponding row in Column C?


Tag : excel , By : Gianluca Riccardi
Date : March 29 2020, 07:55 AM
like below fixes the issue I would advise against VBA when you can achieve the required results easily with worksheet formulas
Put the following formula into cell D1 and drag down
{=IF(SUM(IFERROR(FIND($A$1:$A$4,$B1),0)),$C1,"")}

Python, dataframes : duplicating elements in a column of lists and attributing to them rows in another column


Tag : python , By : BooTeK
Date : March 29 2020, 07:55 AM
this one helps. I think you can use numpy.repeat for repeat values by legths by str.len and flat values of nested lists by chain:
from itertools import chain

df2 = pd.DataFrame({
        "columns_B": np.repeat(df.columns_B.values, df.columns_A.str.len()),
        "columns_A": list(chain.from_iterable(df.columns_A))})

print (df2)
   columns_A        columns_B
0          1      [bob, mary]
1          2      [bob, mary]
2          3      [bob, mary]
3          4      [bob, mary]
4          7  [banana, apple]
5          9  [banana, apple]
df2 = pd.DataFrame(df.columns_A.values.tolist())
        .stack()
        .reset_index(level=1, drop=True)
        .astype(int)
        .rename('columns_A')
print (df2)
0    1
0    2
0    3
0    4
1    7
1    9
Name: columns_A, dtype: int32

print (df.drop('columns_A', axis=1).join(df2).reset_index(drop=True))
         columns_B  columns_A
0      [bob, mary]          1
1      [bob, mary]          2
2      [bob, mary]          3
3      [bob, mary]          4
4  [banana, apple]          7
5  [banana, apple]          9

Duplicating rows in R dataframe based on splitting cells in a column


Tag : r , By : Jeskl
Date : March 29 2020, 07:55 AM
With these it helps I have a dataframe as such: , If it is a string column, we can make use separate_rows
library(tidyverse)
df1 %>%
   separate_rows(Col2, convert = TRUE)
#   Col1 Col2
#1    a    2
#2    b    1
#3    b    3
#4    c    4
df1 <- structure(list(Col1 = c("a", "b", "c"), Col2 = c("2", "1, 3", 
"4")), class = "data.frame", row.names = c(NA, -3L))
Related Posts Related QUESTIONS :
  • 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?
  • How do I easily sum up values in different columns?
  • Reading numeric Date value from CSV file to data.frame in "R"
  • R programming: creating a stacked bar graph, with variable colors for each stacked bar
  • How to identify all columns that contain binary representation
  • Filter different groups by different factor levels
  • Saving .xlsx file to disc, form http post request
  • Add an "all" option under the filter that selects the number of rows displayed in a datatable
  • How to select second column of every xts in list
  • Generate a frequency dataframe out of an input dataframe
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com