rbind a type of column from a list of tibbles2019 Community Moderator ElectionHow to sort a dataframe by multiple column(s)?Drop data frame columns by nameWhy is rbindlist “better” than rbind?Simplest way to get rbind to ignore column namesR: rbind a list of data.frames with different columns in different data framesrbind dataframes from a listR lapply using stringi and rbindrbind and cbind lists togetherrbind all data frames with common names based on list using lapplyR — Cannot rbind In lapply function
How exactly does an Ethernet collision happen in the cable, since nodes use different circuits for Tx and Rx?
Did Amazon pay $0 in taxes last year?
Which situations would cause a company to ground or recall a aircraft series?
Is it possible that a question has only two answers?
Why couldn't the separatists legally leave the Republic?
Source permutation
Professor forcing me to attend a conference, I can't afford even with 50% funding
How can I manipulate the output of Information?
Outlet with 3 sets of wires
How does Ehrenfest's theorem apply to the quantum harmonic oscillator?
PTIJ: Why does only a Shor Tam ask at the Seder, and not a Shor Mu'ad?
Expressing logarithmic equations without logs
Can one live in the U.S. and not use a credit card?
Does "Until when" sound natural for native speakers?
Plausibility of Mushroom Buildings
Signed and unsigned numbers
Why does Central Limit Theorem break down in my simulation?
When a wind turbine does not produce enough electricity how does the power company compensate for the loss?
Would an aboleth's Phantasmal Force lair action be affected by Counterspell, Dispel Magic, and/or Slow?
Windows Server Datacenter Edition - Unlimited Virtual Machines
How to write a chaotic neutral protagonist and prevent my readers from thinking they are evil?
What ability score modifier does a javelin's damage use?
Why does cron require MTA for logging?
Giving a career talk in my old university, how prominently should I tell students my salary?
rbind a type of column from a list of tibbles
2019 Community Moderator ElectionHow to sort a dataframe by multiple column(s)?Drop data frame columns by nameWhy is rbindlist “better” than rbind?Simplest way to get rbind to ignore column namesR: rbind a list of data.frames with different columns in different data framesrbind dataframes from a listR lapply using stringi and rbindrbind and cbind lists togetherrbind all data frames with common names based on list using lapplyR — Cannot rbind In lapply function
I have four excel files that I have loaded onto R using list.files, and have used lapply to read them in.
My code is:
my_files <- list.files(pattern = '*.xlsx')
my_list <- lapply(my_files ,read_excel)
The files contain many different columns:
lapply(my_list ,colnames)
> lapply(my_list ,colnames)
[[1]]
[1] "JobCard Branch" "Customer Name" "Primary Contact No" "Alt No 1"
[5] "Alt No 2" "Reg No"
[[2]]
[1] "CUSTOMER" "Primary Contact No" "Alt No 1" "REG NO#"
[5] "VehModel" "Last Service Outlet"
[[3]]
[1] "Company Name" "JobCard Branch" "Service_Branch"
[4] "HUB" "Customer Code" "Address"
[7] "Address Line2" "Primary Contact No" "Alt No 1"
[10] "Alt No 2" "Alt No 3" "Zip"
[13] "Source" "City" "Vehicle Model"
[16] "Make" "Reg No" "Chasis No"
[[4]]
[1] "Last Call Date" "Reg.No" "Model" "Customer Name" "Contact Number" "Booked Outlet"
>
Could someone let me know if I can extract only the registration number columns ("Reg No","REG NO#","Reg No","Reg.No") from all these tibbles using rbind or any other function.
r lapply rbind
add a comment |
I have four excel files that I have loaded onto R using list.files, and have used lapply to read them in.
My code is:
my_files <- list.files(pattern = '*.xlsx')
my_list <- lapply(my_files ,read_excel)
The files contain many different columns:
lapply(my_list ,colnames)
> lapply(my_list ,colnames)
[[1]]
[1] "JobCard Branch" "Customer Name" "Primary Contact No" "Alt No 1"
[5] "Alt No 2" "Reg No"
[[2]]
[1] "CUSTOMER" "Primary Contact No" "Alt No 1" "REG NO#"
[5] "VehModel" "Last Service Outlet"
[[3]]
[1] "Company Name" "JobCard Branch" "Service_Branch"
[4] "HUB" "Customer Code" "Address"
[7] "Address Line2" "Primary Contact No" "Alt No 1"
[10] "Alt No 2" "Alt No 3" "Zip"
[13] "Source" "City" "Vehicle Model"
[16] "Make" "Reg No" "Chasis No"
[[4]]
[1] "Last Call Date" "Reg.No" "Model" "Customer Name" "Contact Number" "Booked Outlet"
>
Could someone let me know if I can extract only the registration number columns ("Reg No","REG NO#","Reg No","Reg.No") from all these tibbles using rbind or any other function.
r lapply rbind
add a comment |
I have four excel files that I have loaded onto R using list.files, and have used lapply to read them in.
My code is:
my_files <- list.files(pattern = '*.xlsx')
my_list <- lapply(my_files ,read_excel)
The files contain many different columns:
lapply(my_list ,colnames)
> lapply(my_list ,colnames)
[[1]]
[1] "JobCard Branch" "Customer Name" "Primary Contact No" "Alt No 1"
[5] "Alt No 2" "Reg No"
[[2]]
[1] "CUSTOMER" "Primary Contact No" "Alt No 1" "REG NO#"
[5] "VehModel" "Last Service Outlet"
[[3]]
[1] "Company Name" "JobCard Branch" "Service_Branch"
[4] "HUB" "Customer Code" "Address"
[7] "Address Line2" "Primary Contact No" "Alt No 1"
[10] "Alt No 2" "Alt No 3" "Zip"
[13] "Source" "City" "Vehicle Model"
[16] "Make" "Reg No" "Chasis No"
[[4]]
[1] "Last Call Date" "Reg.No" "Model" "Customer Name" "Contact Number" "Booked Outlet"
>
Could someone let me know if I can extract only the registration number columns ("Reg No","REG NO#","Reg No","Reg.No") from all these tibbles using rbind or any other function.
r lapply rbind
I have four excel files that I have loaded onto R using list.files, and have used lapply to read them in.
My code is:
my_files <- list.files(pattern = '*.xlsx')
my_list <- lapply(my_files ,read_excel)
The files contain many different columns:
lapply(my_list ,colnames)
> lapply(my_list ,colnames)
[[1]]
[1] "JobCard Branch" "Customer Name" "Primary Contact No" "Alt No 1"
[5] "Alt No 2" "Reg No"
[[2]]
[1] "CUSTOMER" "Primary Contact No" "Alt No 1" "REG NO#"
[5] "VehModel" "Last Service Outlet"
[[3]]
[1] "Company Name" "JobCard Branch" "Service_Branch"
[4] "HUB" "Customer Code" "Address"
[7] "Address Line2" "Primary Contact No" "Alt No 1"
[10] "Alt No 2" "Alt No 3" "Zip"
[13] "Source" "City" "Vehicle Model"
[16] "Make" "Reg No" "Chasis No"
[[4]]
[1] "Last Call Date" "Reg.No" "Model" "Customer Name" "Contact Number" "Booked Outlet"
>
Could someone let me know if I can extract only the registration number columns ("Reg No","REG NO#","Reg No","Reg.No") from all these tibbles using rbind or any other function.
r lapply rbind
r lapply rbind
asked Mar 7 at 5:23
Shane BondShane Bond
724
724
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
We could create a vector of column names (cols
) which we want to extract and then use lapply
to loop over list of dataframes and subset the columns which match cols
.
cols <- c("Reg No","REG NO#","Reg No","Reg.No")
data.frame(unlist(lapply(my_list, function(x)
x[names(x) %in% cols]), use.names = FALSE))
A reproducible example
df1 <- data.frame(a = 1:5, b = 2:6)
df2 <- data.frame(a1 = 1:4, new_s = 2:5)
df3 <- data.frame(abc = 1:4)
list_df <- list(df1, df2, df3)
cols <- c("a", "a1", "abc")
data.frame(new = unlist(lapply(list_df, function(x)
x[names(x) %in% cols]),use.names = FALSE))
# new
# 1 1
# 2 2
# 3 3
# 4 4
# 5 5
# 6 1
# 7 2
# 8 3
# 9 4
#10 1
#11 2
#12 3
#13 4
I am able to get the Reg No columns from each DF/tibble. But I need to combine them into one tibble or even a vector. The output is another list of 4 tibbles. The output should be from your above example: 1 2 3 4 5 1 2 3 4 1 2 3 4
– Shane Bond
Mar 7 at 5:56
1
@ShaneBond You couldunlist
and wrap it indata.frame
ortibble
according to your preference. Updated the answer.
– Ronak Shah
Mar 7 at 6:04
add a comment |
You may try using grep
in case insensitive mode:
lapply(my_list, function(x)
y <- colnames(x)
y[grep("\breg\b", y, ignore.case=TRUE)]
)
This uses the regex pattern breg]b
in case insensitive mode, to find the column names which match what you want.
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%2f55036606%2frbind-a-type-of-column-from-a-list-of-tibbles%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
We could create a vector of column names (cols
) which we want to extract and then use lapply
to loop over list of dataframes and subset the columns which match cols
.
cols <- c("Reg No","REG NO#","Reg No","Reg.No")
data.frame(unlist(lapply(my_list, function(x)
x[names(x) %in% cols]), use.names = FALSE))
A reproducible example
df1 <- data.frame(a = 1:5, b = 2:6)
df2 <- data.frame(a1 = 1:4, new_s = 2:5)
df3 <- data.frame(abc = 1:4)
list_df <- list(df1, df2, df3)
cols <- c("a", "a1", "abc")
data.frame(new = unlist(lapply(list_df, function(x)
x[names(x) %in% cols]),use.names = FALSE))
# new
# 1 1
# 2 2
# 3 3
# 4 4
# 5 5
# 6 1
# 7 2
# 8 3
# 9 4
#10 1
#11 2
#12 3
#13 4
I am able to get the Reg No columns from each DF/tibble. But I need to combine them into one tibble or even a vector. The output is another list of 4 tibbles. The output should be from your above example: 1 2 3 4 5 1 2 3 4 1 2 3 4
– Shane Bond
Mar 7 at 5:56
1
@ShaneBond You couldunlist
and wrap it indata.frame
ortibble
according to your preference. Updated the answer.
– Ronak Shah
Mar 7 at 6:04
add a comment |
We could create a vector of column names (cols
) which we want to extract and then use lapply
to loop over list of dataframes and subset the columns which match cols
.
cols <- c("Reg No","REG NO#","Reg No","Reg.No")
data.frame(unlist(lapply(my_list, function(x)
x[names(x) %in% cols]), use.names = FALSE))
A reproducible example
df1 <- data.frame(a = 1:5, b = 2:6)
df2 <- data.frame(a1 = 1:4, new_s = 2:5)
df3 <- data.frame(abc = 1:4)
list_df <- list(df1, df2, df3)
cols <- c("a", "a1", "abc")
data.frame(new = unlist(lapply(list_df, function(x)
x[names(x) %in% cols]),use.names = FALSE))
# new
# 1 1
# 2 2
# 3 3
# 4 4
# 5 5
# 6 1
# 7 2
# 8 3
# 9 4
#10 1
#11 2
#12 3
#13 4
I am able to get the Reg No columns from each DF/tibble. But I need to combine them into one tibble or even a vector. The output is another list of 4 tibbles. The output should be from your above example: 1 2 3 4 5 1 2 3 4 1 2 3 4
– Shane Bond
Mar 7 at 5:56
1
@ShaneBond You couldunlist
and wrap it indata.frame
ortibble
according to your preference. Updated the answer.
– Ronak Shah
Mar 7 at 6:04
add a comment |
We could create a vector of column names (cols
) which we want to extract and then use lapply
to loop over list of dataframes and subset the columns which match cols
.
cols <- c("Reg No","REG NO#","Reg No","Reg.No")
data.frame(unlist(lapply(my_list, function(x)
x[names(x) %in% cols]), use.names = FALSE))
A reproducible example
df1 <- data.frame(a = 1:5, b = 2:6)
df2 <- data.frame(a1 = 1:4, new_s = 2:5)
df3 <- data.frame(abc = 1:4)
list_df <- list(df1, df2, df3)
cols <- c("a", "a1", "abc")
data.frame(new = unlist(lapply(list_df, function(x)
x[names(x) %in% cols]),use.names = FALSE))
# new
# 1 1
# 2 2
# 3 3
# 4 4
# 5 5
# 6 1
# 7 2
# 8 3
# 9 4
#10 1
#11 2
#12 3
#13 4
We could create a vector of column names (cols
) which we want to extract and then use lapply
to loop over list of dataframes and subset the columns which match cols
.
cols <- c("Reg No","REG NO#","Reg No","Reg.No")
data.frame(unlist(lapply(my_list, function(x)
x[names(x) %in% cols]), use.names = FALSE))
A reproducible example
df1 <- data.frame(a = 1:5, b = 2:6)
df2 <- data.frame(a1 = 1:4, new_s = 2:5)
df3 <- data.frame(abc = 1:4)
list_df <- list(df1, df2, df3)
cols <- c("a", "a1", "abc")
data.frame(new = unlist(lapply(list_df, function(x)
x[names(x) %in% cols]),use.names = FALSE))
# new
# 1 1
# 2 2
# 3 3
# 4 4
# 5 5
# 6 1
# 7 2
# 8 3
# 9 4
#10 1
#11 2
#12 3
#13 4
edited Mar 7 at 6:03
answered Mar 7 at 5:29
Ronak ShahRonak Shah
39.8k104162
39.8k104162
I am able to get the Reg No columns from each DF/tibble. But I need to combine them into one tibble or even a vector. The output is another list of 4 tibbles. The output should be from your above example: 1 2 3 4 5 1 2 3 4 1 2 3 4
– Shane Bond
Mar 7 at 5:56
1
@ShaneBond You couldunlist
and wrap it indata.frame
ortibble
according to your preference. Updated the answer.
– Ronak Shah
Mar 7 at 6:04
add a comment |
I am able to get the Reg No columns from each DF/tibble. But I need to combine them into one tibble or even a vector. The output is another list of 4 tibbles. The output should be from your above example: 1 2 3 4 5 1 2 3 4 1 2 3 4
– Shane Bond
Mar 7 at 5:56
1
@ShaneBond You couldunlist
and wrap it indata.frame
ortibble
according to your preference. Updated the answer.
– Ronak Shah
Mar 7 at 6:04
I am able to get the Reg No columns from each DF/tibble. But I need to combine them into one tibble or even a vector. The output is another list of 4 tibbles. The output should be from your above example: 1 2 3 4 5 1 2 3 4 1 2 3 4
– Shane Bond
Mar 7 at 5:56
I am able to get the Reg No columns from each DF/tibble. But I need to combine them into one tibble or even a vector. The output is another list of 4 tibbles. The output should be from your above example: 1 2 3 4 5 1 2 3 4 1 2 3 4
– Shane Bond
Mar 7 at 5:56
1
1
@ShaneBond You could
unlist
and wrap it in data.frame
or tibble
according to your preference. Updated the answer.– Ronak Shah
Mar 7 at 6:04
@ShaneBond You could
unlist
and wrap it in data.frame
or tibble
according to your preference. Updated the answer.– Ronak Shah
Mar 7 at 6:04
add a comment |
You may try using grep
in case insensitive mode:
lapply(my_list, function(x)
y <- colnames(x)
y[grep("\breg\b", y, ignore.case=TRUE)]
)
This uses the regex pattern breg]b
in case insensitive mode, to find the column names which match what you want.
add a comment |
You may try using grep
in case insensitive mode:
lapply(my_list, function(x)
y <- colnames(x)
y[grep("\breg\b", y, ignore.case=TRUE)]
)
This uses the regex pattern breg]b
in case insensitive mode, to find the column names which match what you want.
add a comment |
You may try using grep
in case insensitive mode:
lapply(my_list, function(x)
y <- colnames(x)
y[grep("\breg\b", y, ignore.case=TRUE)]
)
This uses the regex pattern breg]b
in case insensitive mode, to find the column names which match what you want.
You may try using grep
in case insensitive mode:
lapply(my_list, function(x)
y <- colnames(x)
y[grep("\breg\b", y, ignore.case=TRUE)]
)
This uses the regex pattern breg]b
in case insensitive mode, to find the column names which match what you want.
answered Mar 7 at 5:29
Tim BiegeleisenTim Biegeleisen
231k1396152
231k1396152
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%2f55036606%2frbind-a-type-of-column-from-a-list-of-tibbles%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