R script merge 2 rows Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How to merge two dictionaries in a single expression?How to merge a specific commit in GitHow to join (merge) data frames (inner, outer, left, right)How do you merge two Git repositories?How to merge two arrays in JavaScript and de-duplicate itemsGrouping functions (tapply, by, aggregate) and the *apply familyDrop data frame columns by nameRemove rows with all or some NAs (missing values) in data.frameAdding new column to existing DataFrame in Python pandasHow to iterate over rows in a DataFrame in Pandas?
What does it mean that physics no longer uses mechanical models to describe phenomena?
How do living politicians protect their readily obtainable signatures from misuse?
Why should I vote and accept answers?
The code below, is it ill-formed NDR or is it well formed?
Does the Weapon Master feat grant you a fighting style?
A term for a woman complaining about things/begging in a cute/childish way
When a candle burns, why does the top of wick glow if bottom of flame is hottest?
Why weren't discrete x86 CPUs ever used in game hardware?
What do you call the main part of a joke?
Find 108 by using 3,4,6
What is this clumpy 20-30cm high yellow-flowered plant?
How does light 'choose' between wave and particle behaviour?
Central Vacuuming: Is it worth it, and how does it compare to normal vacuuming?
AppleTVs create a chatty alternate WiFi network
Do wooden building fires get hotter than 600°C?
How can I reduce the gap between left and right of cdot with a macro?
Can the Great Weapon Master feat's "Power Attack" apply to attacks from the Spiritual Weapon spell?
Do I really need to have a message in a novel to appeal to readers?
What was the first language to use conditional keywords?
Do any jurisdictions seriously consider reclassifying social media websites as publishers?
Question about debouncing - delay of state change
What is the font for "b" letter?
Project Euler #1 in C++
How could we fake a moon landing now?
R script merge 2 rows
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How to merge two dictionaries in a single expression?How to merge a specific commit in GitHow to join (merge) data frames (inner, outer, left, right)How do you merge two Git repositories?How to merge two arrays in JavaScript and de-duplicate itemsGrouping functions (tapply, by, aggregate) and the *apply familyDrop data frame columns by nameRemove rows with all or some NAs (missing values) in data.frameAdding new column to existing DataFrame in Python pandasHow to iterate over rows in a DataFrame in Pandas?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have one data frame like:
a b c d e f g
1 Car 10/02 01/02 30/02 14 1 NA
2 Car 10/02 07/02 20/02 0 NA 7
I want to get :
a b c d e f g
1 Car 10/02 01/02 20/02 14 1 7
Like a group by (a,b) , select the min Date for c and d, select the max for e and select the non-null for f and g
How can I solve it in R ?
r dataframe merge row
add a comment |
I have one data frame like:
a b c d e f g
1 Car 10/02 01/02 30/02 14 1 NA
2 Car 10/02 07/02 20/02 0 NA 7
I want to get :
a b c d e f g
1 Car 10/02 01/02 20/02 14 1 7
Like a group by (a,b) , select the min Date for c and d, select the max for e and select the non-null for f and g
How can I solve it in R ?
r dataframe merge row
Please share the data usingdput
and the logic
– Sonny
Mar 22 at 10:14
I want to group by (a,b) select the Date min on c and on d select the max number on e select the not null on f and g
– Catapultaa
Mar 22 at 10:35
whats the pattern. in col d you take the second value . in col c you take the first row
– MatthewR
Mar 22 at 10:36
I take the min Date
– Catapultaa
Mar 22 at 10:38
add a comment |
I have one data frame like:
a b c d e f g
1 Car 10/02 01/02 30/02 14 1 NA
2 Car 10/02 07/02 20/02 0 NA 7
I want to get :
a b c d e f g
1 Car 10/02 01/02 20/02 14 1 7
Like a group by (a,b) , select the min Date for c and d, select the max for e and select the non-null for f and g
How can I solve it in R ?
r dataframe merge row
I have one data frame like:
a b c d e f g
1 Car 10/02 01/02 30/02 14 1 NA
2 Car 10/02 07/02 20/02 0 NA 7
I want to get :
a b c d e f g
1 Car 10/02 01/02 20/02 14 1 7
Like a group by (a,b) , select the min Date for c and d, select the max for e and select the non-null for f and g
How can I solve it in R ?
r dataframe merge row
r dataframe merge row
edited Mar 22 at 10:41
Catapultaa
asked Mar 22 at 10:12
CatapultaaCatapultaa
357
357
Please share the data usingdput
and the logic
– Sonny
Mar 22 at 10:14
I want to group by (a,b) select the Date min on c and on d select the max number on e select the not null on f and g
– Catapultaa
Mar 22 at 10:35
whats the pattern. in col d you take the second value . in col c you take the first row
– MatthewR
Mar 22 at 10:36
I take the min Date
– Catapultaa
Mar 22 at 10:38
add a comment |
Please share the data usingdput
and the logic
– Sonny
Mar 22 at 10:14
I want to group by (a,b) select the Date min on c and on d select the max number on e select the not null on f and g
– Catapultaa
Mar 22 at 10:35
whats the pattern. in col d you take the second value . in col c you take the first row
– MatthewR
Mar 22 at 10:36
I take the min Date
– Catapultaa
Mar 22 at 10:38
Please share the data using
dput
and the logic– Sonny
Mar 22 at 10:14
Please share the data using
dput
and the logic– Sonny
Mar 22 at 10:14
I want to group by (a,b) select the Date min on c and on d select the max number on e select the not null on f and g
– Catapultaa
Mar 22 at 10:35
I want to group by (a,b) select the Date min on c and on d select the max number on e select the not null on f and g
– Catapultaa
Mar 22 at 10:35
whats the pattern. in col d you take the second value . in col c you take the first row
– MatthewR
Mar 22 at 10:36
whats the pattern. in col d you take the second value . in col c you take the first row
– MatthewR
Mar 22 at 10:36
I take the min Date
– Catapultaa
Mar 22 at 10:38
I take the min Date
– Catapultaa
Mar 22 at 10:38
add a comment |
3 Answers
3
active
oldest
votes
Using dplyr
we can group_by
a
and b
, convert c
and d
to actual dates using dmy
from lubridate
and select the minimum date, select max
imum value of e
and non-NA value of f
and g
.
library(dplyr)
library(lubridate)
df %>%
group_by(a, b) %>%
summarise(c = c[which.min(dmy(paste0(c, "/19")))],
d = d[which.min(dmy(paste0(d, "/19")))],
e = max(e),
f = f[!is.na(f)],
g = g[!is.na(g)])
# a b c d e f g
# <fct> <fct> <fct> <fct> <dbl> <int> <int>
#1 Car 10/02 01/02 20/02 14 1 7
As shown in the example, I am assuming you would have only one non-NA value for f
and g
if you have more than one then use which.max
to select the first non-NA value from those columns.
add a comment |
Use library dplyr, let's say your dataframe is df. First thing is replace NA by 0
df[is.na(df)] <- 0 # you can now directly take sum of f and g column in group by
library(dplyr)
df_1 <- df %>% group_by(a,b) %>% summarise(c = min(c),d = min(d),e = max(e),f = sum(f),g = sum(g))
min
would not work for dates. (columnc
andd
). Here it works becausemin(c("01", "07"))
gives you "01" but it would give you unexpected results formin(c("11", "7"))
. You need to convert it to date first to usemin
,max
functions.
– Ronak Shah
Mar 22 at 10:50
add a comment |
using data.table. I had to change one of your dates because there is no feb 30th
x <- data.frame(
a= c( "Car","Car"), b=c("10/20","10/20"), c=c("01/02","07/02"),d=c("28/02","20/02"), e=c(14,0), f=c(1, NA), g=c(NA,7))
library( data.table) # you may need to install
x <- data.table( x )
#convert to dates
x$c <- as.Date(x$c, "%d/%m")
x$d <- as.Date(x$d, "%d/%m")
# group as you specfied
x[ ,
.(
c = min( c ),
d = min( d ),
e = max( e ),
f= max( f , na.rm=T),
g= max( g , na.rm=T)
) ,
by= c( "a","b")
]
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55297335%2fr-script-merge-2-rows%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Using dplyr
we can group_by
a
and b
, convert c
and d
to actual dates using dmy
from lubridate
and select the minimum date, select max
imum value of e
and non-NA value of f
and g
.
library(dplyr)
library(lubridate)
df %>%
group_by(a, b) %>%
summarise(c = c[which.min(dmy(paste0(c, "/19")))],
d = d[which.min(dmy(paste0(d, "/19")))],
e = max(e),
f = f[!is.na(f)],
g = g[!is.na(g)])
# a b c d e f g
# <fct> <fct> <fct> <fct> <dbl> <int> <int>
#1 Car 10/02 01/02 20/02 14 1 7
As shown in the example, I am assuming you would have only one non-NA value for f
and g
if you have more than one then use which.max
to select the first non-NA value from those columns.
add a comment |
Using dplyr
we can group_by
a
and b
, convert c
and d
to actual dates using dmy
from lubridate
and select the minimum date, select max
imum value of e
and non-NA value of f
and g
.
library(dplyr)
library(lubridate)
df %>%
group_by(a, b) %>%
summarise(c = c[which.min(dmy(paste0(c, "/19")))],
d = d[which.min(dmy(paste0(d, "/19")))],
e = max(e),
f = f[!is.na(f)],
g = g[!is.na(g)])
# a b c d e f g
# <fct> <fct> <fct> <fct> <dbl> <int> <int>
#1 Car 10/02 01/02 20/02 14 1 7
As shown in the example, I am assuming you would have only one non-NA value for f
and g
if you have more than one then use which.max
to select the first non-NA value from those columns.
add a comment |
Using dplyr
we can group_by
a
and b
, convert c
and d
to actual dates using dmy
from lubridate
and select the minimum date, select max
imum value of e
and non-NA value of f
and g
.
library(dplyr)
library(lubridate)
df %>%
group_by(a, b) %>%
summarise(c = c[which.min(dmy(paste0(c, "/19")))],
d = d[which.min(dmy(paste0(d, "/19")))],
e = max(e),
f = f[!is.na(f)],
g = g[!is.na(g)])
# a b c d e f g
# <fct> <fct> <fct> <fct> <dbl> <int> <int>
#1 Car 10/02 01/02 20/02 14 1 7
As shown in the example, I am assuming you would have only one non-NA value for f
and g
if you have more than one then use which.max
to select the first non-NA value from those columns.
Using dplyr
we can group_by
a
and b
, convert c
and d
to actual dates using dmy
from lubridate
and select the minimum date, select max
imum value of e
and non-NA value of f
and g
.
library(dplyr)
library(lubridate)
df %>%
group_by(a, b) %>%
summarise(c = c[which.min(dmy(paste0(c, "/19")))],
d = d[which.min(dmy(paste0(d, "/19")))],
e = max(e),
f = f[!is.na(f)],
g = g[!is.na(g)])
# a b c d e f g
# <fct> <fct> <fct> <fct> <dbl> <int> <int>
#1 Car 10/02 01/02 20/02 14 1 7
As shown in the example, I am assuming you would have only one non-NA value for f
and g
if you have more than one then use which.max
to select the first non-NA value from those columns.
answered Mar 22 at 10:41
Ronak ShahRonak Shah
48.2k104369
48.2k104369
add a comment |
add a comment |
Use library dplyr, let's say your dataframe is df. First thing is replace NA by 0
df[is.na(df)] <- 0 # you can now directly take sum of f and g column in group by
library(dplyr)
df_1 <- df %>% group_by(a,b) %>% summarise(c = min(c),d = min(d),e = max(e),f = sum(f),g = sum(g))
min
would not work for dates. (columnc
andd
). Here it works becausemin(c("01", "07"))
gives you "01" but it would give you unexpected results formin(c("11", "7"))
. You need to convert it to date first to usemin
,max
functions.
– Ronak Shah
Mar 22 at 10:50
add a comment |
Use library dplyr, let's say your dataframe is df. First thing is replace NA by 0
df[is.na(df)] <- 0 # you can now directly take sum of f and g column in group by
library(dplyr)
df_1 <- df %>% group_by(a,b) %>% summarise(c = min(c),d = min(d),e = max(e),f = sum(f),g = sum(g))
min
would not work for dates. (columnc
andd
). Here it works becausemin(c("01", "07"))
gives you "01" but it would give you unexpected results formin(c("11", "7"))
. You need to convert it to date first to usemin
,max
functions.
– Ronak Shah
Mar 22 at 10:50
add a comment |
Use library dplyr, let's say your dataframe is df. First thing is replace NA by 0
df[is.na(df)] <- 0 # you can now directly take sum of f and g column in group by
library(dplyr)
df_1 <- df %>% group_by(a,b) %>% summarise(c = min(c),d = min(d),e = max(e),f = sum(f),g = sum(g))
Use library dplyr, let's say your dataframe is df. First thing is replace NA by 0
df[is.na(df)] <- 0 # you can now directly take sum of f and g column in group by
library(dplyr)
df_1 <- df %>% group_by(a,b) %>% summarise(c = min(c),d = min(d),e = max(e),f = sum(f),g = sum(g))
answered Mar 22 at 10:43
adjustedR2adjustedR2
1413
1413
min
would not work for dates. (columnc
andd
). Here it works becausemin(c("01", "07"))
gives you "01" but it would give you unexpected results formin(c("11", "7"))
. You need to convert it to date first to usemin
,max
functions.
– Ronak Shah
Mar 22 at 10:50
add a comment |
min
would not work for dates. (columnc
andd
). Here it works becausemin(c("01", "07"))
gives you "01" but it would give you unexpected results formin(c("11", "7"))
. You need to convert it to date first to usemin
,max
functions.
– Ronak Shah
Mar 22 at 10:50
min
would not work for dates. (column c
and d
). Here it works because min(c("01", "07"))
gives you "01" but it would give you unexpected results for min(c("11", "7"))
. You need to convert it to date first to use min
, max
functions.– Ronak Shah
Mar 22 at 10:50
min
would not work for dates. (column c
and d
). Here it works because min(c("01", "07"))
gives you "01" but it would give you unexpected results for min(c("11", "7"))
. You need to convert it to date first to use min
, max
functions.– Ronak Shah
Mar 22 at 10:50
add a comment |
using data.table. I had to change one of your dates because there is no feb 30th
x <- data.frame(
a= c( "Car","Car"), b=c("10/20","10/20"), c=c("01/02","07/02"),d=c("28/02","20/02"), e=c(14,0), f=c(1, NA), g=c(NA,7))
library( data.table) # you may need to install
x <- data.table( x )
#convert to dates
x$c <- as.Date(x$c, "%d/%m")
x$d <- as.Date(x$d, "%d/%m")
# group as you specfied
x[ ,
.(
c = min( c ),
d = min( d ),
e = max( e ),
f= max( f , na.rm=T),
g= max( g , na.rm=T)
) ,
by= c( "a","b")
]
add a comment |
using data.table. I had to change one of your dates because there is no feb 30th
x <- data.frame(
a= c( "Car","Car"), b=c("10/20","10/20"), c=c("01/02","07/02"),d=c("28/02","20/02"), e=c(14,0), f=c(1, NA), g=c(NA,7))
library( data.table) # you may need to install
x <- data.table( x )
#convert to dates
x$c <- as.Date(x$c, "%d/%m")
x$d <- as.Date(x$d, "%d/%m")
# group as you specfied
x[ ,
.(
c = min( c ),
d = min( d ),
e = max( e ),
f= max( f , na.rm=T),
g= max( g , na.rm=T)
) ,
by= c( "a","b")
]
add a comment |
using data.table. I had to change one of your dates because there is no feb 30th
x <- data.frame(
a= c( "Car","Car"), b=c("10/20","10/20"), c=c("01/02","07/02"),d=c("28/02","20/02"), e=c(14,0), f=c(1, NA), g=c(NA,7))
library( data.table) # you may need to install
x <- data.table( x )
#convert to dates
x$c <- as.Date(x$c, "%d/%m")
x$d <- as.Date(x$d, "%d/%m")
# group as you specfied
x[ ,
.(
c = min( c ),
d = min( d ),
e = max( e ),
f= max( f , na.rm=T),
g= max( g , na.rm=T)
) ,
by= c( "a","b")
]
using data.table. I had to change one of your dates because there is no feb 30th
x <- data.frame(
a= c( "Car","Car"), b=c("10/20","10/20"), c=c("01/02","07/02"),d=c("28/02","20/02"), e=c(14,0), f=c(1, NA), g=c(NA,7))
library( data.table) # you may need to install
x <- data.table( x )
#convert to dates
x$c <- as.Date(x$c, "%d/%m")
x$d <- as.Date(x$d, "%d/%m")
# group as you specfied
x[ ,
.(
c = min( c ),
d = min( d ),
e = max( e ),
f= max( f , na.rm=T),
g= max( g , na.rm=T)
) ,
by= c( "a","b")
]
edited Mar 22 at 10:53
answered Mar 22 at 10:35
MatthewRMatthewR
93011019
93011019
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55297335%2fr-script-merge-2-rows%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please share the data using
dput
and the logic– Sonny
Mar 22 at 10:14
I want to group by (a,b) select the Date min on c and on d select the max number on e select the not null on f and g
– Catapultaa
Mar 22 at 10:35
whats the pattern. in col d you take the second value . in col c you take the first row
– MatthewR
Mar 22 at 10:36
I take the min Date
– Catapultaa
Mar 22 at 10:38