How to add multiple columns using lapply? [duplicate]
This question already has an answer here:
Split text string in a data.table columns
5 answers
I have a data table as below:
dt=data.table(
id=c(1,2,3,4,5,6),
date=c("28 Jul 2009","31 Jul 2009","31 Jul 2009","04 Aug 2009","10 Aug 2009","06 Aug 2009")
)
I want to add three more columns day,month,year to this column.So I tried
dt[,day:=unlist(strsplit(date,"\ "))[1]]
dt[,month:=unlist(strsplit(date,"\ "))[2]]
dt[,year:=unlist(strsplit(date,"\ "))[3]]
But this just appended all columns with same split for the first row.
SoI tried
dt[,day:=lapply(date,function (x) unlist(strsplit(x,"\ "))[1]),]
dt[,month:=lapply(date,function (x) unlist(strsplit(x,"\ "))[2]),]
dt[,year:=lapply(date,function (x) unlist(strsplit(x,"\ "))[3]),]
which worked. But how do I combine all in one line instead of 3 lines and any other efficeint methods.Any help is appreciated
r dplyr data.table
marked as duplicate by Henrik
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 15 '18 at 7:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Split text string in a data.table columns
5 answers
I have a data table as below:
dt=data.table(
id=c(1,2,3,4,5,6),
date=c("28 Jul 2009","31 Jul 2009","31 Jul 2009","04 Aug 2009","10 Aug 2009","06 Aug 2009")
)
I want to add three more columns day,month,year to this column.So I tried
dt[,day:=unlist(strsplit(date,"\ "))[1]]
dt[,month:=unlist(strsplit(date,"\ "))[2]]
dt[,year:=unlist(strsplit(date,"\ "))[3]]
But this just appended all columns with same split for the first row.
SoI tried
dt[,day:=lapply(date,function (x) unlist(strsplit(x,"\ "))[1]),]
dt[,month:=lapply(date,function (x) unlist(strsplit(x,"\ "))[2]),]
dt[,year:=lapply(date,function (x) unlist(strsplit(x,"\ "))[3]),]
which worked. But how do I combine all in one line instead of 3 lines and any other efficeint methods.Any help is appreciated
r dplyr data.table
marked as duplicate by Henrik
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 15 '18 at 7:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Split text string in a data.table columns
5 answers
I have a data table as below:
dt=data.table(
id=c(1,2,3,4,5,6),
date=c("28 Jul 2009","31 Jul 2009","31 Jul 2009","04 Aug 2009","10 Aug 2009","06 Aug 2009")
)
I want to add three more columns day,month,year to this column.So I tried
dt[,day:=unlist(strsplit(date,"\ "))[1]]
dt[,month:=unlist(strsplit(date,"\ "))[2]]
dt[,year:=unlist(strsplit(date,"\ "))[3]]
But this just appended all columns with same split for the first row.
SoI tried
dt[,day:=lapply(date,function (x) unlist(strsplit(x,"\ "))[1]),]
dt[,month:=lapply(date,function (x) unlist(strsplit(x,"\ "))[2]),]
dt[,year:=lapply(date,function (x) unlist(strsplit(x,"\ "))[3]),]
which worked. But how do I combine all in one line instead of 3 lines and any other efficeint methods.Any help is appreciated
r dplyr data.table
This question already has an answer here:
Split text string in a data.table columns
5 answers
I have a data table as below:
dt=data.table(
id=c(1,2,3,4,5,6),
date=c("28 Jul 2009","31 Jul 2009","31 Jul 2009","04 Aug 2009","10 Aug 2009","06 Aug 2009")
)
I want to add three more columns day,month,year to this column.So I tried
dt[,day:=unlist(strsplit(date,"\ "))[1]]
dt[,month:=unlist(strsplit(date,"\ "))[2]]
dt[,year:=unlist(strsplit(date,"\ "))[3]]
But this just appended all columns with same split for the first row.
SoI tried
dt[,day:=lapply(date,function (x) unlist(strsplit(x,"\ "))[1]),]
dt[,month:=lapply(date,function (x) unlist(strsplit(x,"\ "))[2]),]
dt[,year:=lapply(date,function (x) unlist(strsplit(x,"\ "))[3]),]
which worked. But how do I combine all in one line instead of 3 lines and any other efficeint methods.Any help is appreciated
This question already has an answer here:
Split text string in a data.table columns
5 answers
r dplyr data.table
r dplyr data.table
asked Nov 15 '18 at 4:50
RickyRicky
7371429
7371429
marked as duplicate by Henrik
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 15 '18 at 7:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Henrik
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 15 '18 at 7:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You want tstrsplit()
, also from the data table package. From help(tstrsplit)
, it is "a convenient wrapper function to split a column using strsplit
and assign the transposed result to individual columns."
dt[, c("day", "month", "year") := tstrsplit(date, " ")]
dt
# id date day month year
# 1: 1 28 Jul 2009 28 Jul 2009
# 2: 2 31 Jul 2009 31 Jul 2009
# 3: 3 31 Jul 2009 31 Jul 2009
# 4: 4 04 Aug 2009 04 Aug 2009
# 5: 5 10 Aug 2009 10 Aug 2009
# 6: 6 06 Aug 2009 06 Aug 2009
add a comment |
You can easily do it using library lubridate and dplyr
library(data.table)
dt=data.table(
id=c(1,2,3,4,5,6),
date=c("28 Jul 2009","31 Jul 2009","31 Jul 2009","04 Aug 2009","10 Aug 2009","06 Aug 2009")
)
str(dt)
library(lubridate)
dt$date<- dmy(dt$date)
library(dplyr)
mutate(dt, day= day(dt$date),month = month(dt$date),year = year(dt$date))
add a comment |
The issue is that your [1]
indexing is taking the first of 18 elements, not the first of each of a list of 6. Try this:
dt[,day := sapply(strsplit(date, "\s"), `[[`, 1),]
dt
# id date day
# 1: 1 28 Jul 2009 28
# 2: 2 31 Jul 2009 31
# 3: 3 31 Jul 2009 31
# 4: 4 04 Aug 2009 04
# 5: 5 10 Aug 2009 10
# 6: 6 06 Aug 2009 06
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You want tstrsplit()
, also from the data table package. From help(tstrsplit)
, it is "a convenient wrapper function to split a column using strsplit
and assign the transposed result to individual columns."
dt[, c("day", "month", "year") := tstrsplit(date, " ")]
dt
# id date day month year
# 1: 1 28 Jul 2009 28 Jul 2009
# 2: 2 31 Jul 2009 31 Jul 2009
# 3: 3 31 Jul 2009 31 Jul 2009
# 4: 4 04 Aug 2009 04 Aug 2009
# 5: 5 10 Aug 2009 10 Aug 2009
# 6: 6 06 Aug 2009 06 Aug 2009
add a comment |
You want tstrsplit()
, also from the data table package. From help(tstrsplit)
, it is "a convenient wrapper function to split a column using strsplit
and assign the transposed result to individual columns."
dt[, c("day", "month", "year") := tstrsplit(date, " ")]
dt
# id date day month year
# 1: 1 28 Jul 2009 28 Jul 2009
# 2: 2 31 Jul 2009 31 Jul 2009
# 3: 3 31 Jul 2009 31 Jul 2009
# 4: 4 04 Aug 2009 04 Aug 2009
# 5: 5 10 Aug 2009 10 Aug 2009
# 6: 6 06 Aug 2009 06 Aug 2009
add a comment |
You want tstrsplit()
, also from the data table package. From help(tstrsplit)
, it is "a convenient wrapper function to split a column using strsplit
and assign the transposed result to individual columns."
dt[, c("day", "month", "year") := tstrsplit(date, " ")]
dt
# id date day month year
# 1: 1 28 Jul 2009 28 Jul 2009
# 2: 2 31 Jul 2009 31 Jul 2009
# 3: 3 31 Jul 2009 31 Jul 2009
# 4: 4 04 Aug 2009 04 Aug 2009
# 5: 5 10 Aug 2009 10 Aug 2009
# 6: 6 06 Aug 2009 06 Aug 2009
You want tstrsplit()
, also from the data table package. From help(tstrsplit)
, it is "a convenient wrapper function to split a column using strsplit
and assign the transposed result to individual columns."
dt[, c("day", "month", "year") := tstrsplit(date, " ")]
dt
# id date day month year
# 1: 1 28 Jul 2009 28 Jul 2009
# 2: 2 31 Jul 2009 31 Jul 2009
# 3: 3 31 Jul 2009 31 Jul 2009
# 4: 4 04 Aug 2009 04 Aug 2009
# 5: 5 10 Aug 2009 10 Aug 2009
# 6: 6 06 Aug 2009 06 Aug 2009
edited Nov 15 '18 at 5:03
answered Nov 15 '18 at 4:58
Rich ScrivenRich Scriven
77.5k8104173
77.5k8104173
add a comment |
add a comment |
You can easily do it using library lubridate and dplyr
library(data.table)
dt=data.table(
id=c(1,2,3,4,5,6),
date=c("28 Jul 2009","31 Jul 2009","31 Jul 2009","04 Aug 2009","10 Aug 2009","06 Aug 2009")
)
str(dt)
library(lubridate)
dt$date<- dmy(dt$date)
library(dplyr)
mutate(dt, day= day(dt$date),month = month(dt$date),year = year(dt$date))
add a comment |
You can easily do it using library lubridate and dplyr
library(data.table)
dt=data.table(
id=c(1,2,3,4,5,6),
date=c("28 Jul 2009","31 Jul 2009","31 Jul 2009","04 Aug 2009","10 Aug 2009","06 Aug 2009")
)
str(dt)
library(lubridate)
dt$date<- dmy(dt$date)
library(dplyr)
mutate(dt, day= day(dt$date),month = month(dt$date),year = year(dt$date))
add a comment |
You can easily do it using library lubridate and dplyr
library(data.table)
dt=data.table(
id=c(1,2,3,4,5,6),
date=c("28 Jul 2009","31 Jul 2009","31 Jul 2009","04 Aug 2009","10 Aug 2009","06 Aug 2009")
)
str(dt)
library(lubridate)
dt$date<- dmy(dt$date)
library(dplyr)
mutate(dt, day= day(dt$date),month = month(dt$date),year = year(dt$date))
You can easily do it using library lubridate and dplyr
library(data.table)
dt=data.table(
id=c(1,2,3,4,5,6),
date=c("28 Jul 2009","31 Jul 2009","31 Jul 2009","04 Aug 2009","10 Aug 2009","06 Aug 2009")
)
str(dt)
library(lubridate)
dt$date<- dmy(dt$date)
library(dplyr)
mutate(dt, day= day(dt$date),month = month(dt$date),year = year(dt$date))
answered Nov 15 '18 at 4:55
HunaidkhanHunaidkhan
1,014516
1,014516
add a comment |
add a comment |
The issue is that your [1]
indexing is taking the first of 18 elements, not the first of each of a list of 6. Try this:
dt[,day := sapply(strsplit(date, "\s"), `[[`, 1),]
dt
# id date day
# 1: 1 28 Jul 2009 28
# 2: 2 31 Jul 2009 31
# 3: 3 31 Jul 2009 31
# 4: 4 04 Aug 2009 04
# 5: 5 10 Aug 2009 10
# 6: 6 06 Aug 2009 06
add a comment |
The issue is that your [1]
indexing is taking the first of 18 elements, not the first of each of a list of 6. Try this:
dt[,day := sapply(strsplit(date, "\s"), `[[`, 1),]
dt
# id date day
# 1: 1 28 Jul 2009 28
# 2: 2 31 Jul 2009 31
# 3: 3 31 Jul 2009 31
# 4: 4 04 Aug 2009 04
# 5: 5 10 Aug 2009 10
# 6: 6 06 Aug 2009 06
add a comment |
The issue is that your [1]
indexing is taking the first of 18 elements, not the first of each of a list of 6. Try this:
dt[,day := sapply(strsplit(date, "\s"), `[[`, 1),]
dt
# id date day
# 1: 1 28 Jul 2009 28
# 2: 2 31 Jul 2009 31
# 3: 3 31 Jul 2009 31
# 4: 4 04 Aug 2009 04
# 5: 5 10 Aug 2009 10
# 6: 6 06 Aug 2009 06
The issue is that your [1]
indexing is taking the first of 18 elements, not the first of each of a list of 6. Try this:
dt[,day := sapply(strsplit(date, "\s"), `[[`, 1),]
dt
# id date day
# 1: 1 28 Jul 2009 28
# 2: 2 31 Jul 2009 31
# 3: 3 31 Jul 2009 31
# 4: 4 04 Aug 2009 04
# 5: 5 10 Aug 2009 10
# 6: 6 06 Aug 2009 06
answered Nov 15 '18 at 4:59
r2evansr2evans
28k33159
28k33159
add a comment |
add a comment |