separate column with unknown name
I have a data frame like this:
structure(list(header = 1:10, ST.adk.fumC.gyrB.icd.mdh.purA.recA = c(" 10 10 11 4 8 8 8 2",
" 48 6 11 4 8 8 8 2", " 58 6 4 4 16 24 8 14", " 88* 6* 4 12 1 20 12 7",
" 117 20 45 41 43 5 32 2", " 7036 526 7 1 1 8 71 6", " 101 43 41 15 18 11 7 6",
" 3595 112 11 5 12 8 88 86", " 117 20 45 41 43 5 32 2", " 744 10 11 135 8 8 8 2"
)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))
What I want to do is to split the second column into separate columns, separated by the "." in the column name. However, it's not always known what the name of the column is, which is why I cannot use the name of the column in dplyr' "separate" function.
I tried the following:
library(dplyr)
library(stringr)
library(tidyr)
# get new column names
ids <- unlist(strsplit(names(df)[-1],
split = ".",
fixed = TRUE))
# get name of column to split
split_column <- names(df)[-1]
df %>%
separate(split_column, into = ids, extra = "merge")
This works within the script file I am using, but when i source the script I get the following error:
Error: `var` must evaluate to a single number or a column name, not a character vector
Why does this work when I run it like normal in RStudio, but when I source the script it throws this error?
Also, is this the optimal way of actually splitting a column of unknown name into new columns with unknown names?
I source the script with the following code, in another script file:
system(paste("Rscript script.R", opt$m, opt$o))
Where opt$m and opt$o are directory paths. This works fine with a similar script I have, but with the script above it throws an error.
I was hoping for some kind of function like separate_at, but that doesn't exist as of yet..
r dplyr tidyr
|
show 5 more comments
I have a data frame like this:
structure(list(header = 1:10, ST.adk.fumC.gyrB.icd.mdh.purA.recA = c(" 10 10 11 4 8 8 8 2",
" 48 6 11 4 8 8 8 2", " 58 6 4 4 16 24 8 14", " 88* 6* 4 12 1 20 12 7",
" 117 20 45 41 43 5 32 2", " 7036 526 7 1 1 8 71 6", " 101 43 41 15 18 11 7 6",
" 3595 112 11 5 12 8 88 86", " 117 20 45 41 43 5 32 2", " 744 10 11 135 8 8 8 2"
)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))
What I want to do is to split the second column into separate columns, separated by the "." in the column name. However, it's not always known what the name of the column is, which is why I cannot use the name of the column in dplyr' "separate" function.
I tried the following:
library(dplyr)
library(stringr)
library(tidyr)
# get new column names
ids <- unlist(strsplit(names(df)[-1],
split = ".",
fixed = TRUE))
# get name of column to split
split_column <- names(df)[-1]
df %>%
separate(split_column, into = ids, extra = "merge")
This works within the script file I am using, but when i source the script I get the following error:
Error: `var` must evaluate to a single number or a column name, not a character vector
Why does this work when I run it like normal in RStudio, but when I source the script it throws this error?
Also, is this the optimal way of actually splitting a column of unknown name into new columns with unknown names?
I source the script with the following code, in another script file:
system(paste("Rscript script.R", opt$m, opt$o))
Where opt$m and opt$o are directory paths. This works fine with a similar script I have, but with the script above it throws an error.
I was hoping for some kind of function like separate_at, but that doesn't exist as of yet..
r dplyr tidyr
Works fine for me: R version 3.4.2, tidyr_0.8.1 dplyr_0.7.7
– zx8754
Nov 23 '18 at 12:54
You might have different versions of R or packages, when using Rstudio vs using sourcing the script.
– zx8754
Nov 23 '18 at 12:55
@zx8754 yes I think that must be the case - I will check out the versions
– Haakonkas
Nov 23 '18 at 12:59
Cannot reproduce. Two things: you might want to run your split column through trimws() before separating, otherwise your first column will be blank with the data you gave. Second - consider callingtidyr::separate()
, as your script maybe accessing a different package for some reason.
– iod
Nov 23 '18 at 13:00
(also, separate is fromtidyr
, notdplyr
)
– iod
Nov 23 '18 at 13:01
|
show 5 more comments
I have a data frame like this:
structure(list(header = 1:10, ST.adk.fumC.gyrB.icd.mdh.purA.recA = c(" 10 10 11 4 8 8 8 2",
" 48 6 11 4 8 8 8 2", " 58 6 4 4 16 24 8 14", " 88* 6* 4 12 1 20 12 7",
" 117 20 45 41 43 5 32 2", " 7036 526 7 1 1 8 71 6", " 101 43 41 15 18 11 7 6",
" 3595 112 11 5 12 8 88 86", " 117 20 45 41 43 5 32 2", " 744 10 11 135 8 8 8 2"
)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))
What I want to do is to split the second column into separate columns, separated by the "." in the column name. However, it's not always known what the name of the column is, which is why I cannot use the name of the column in dplyr' "separate" function.
I tried the following:
library(dplyr)
library(stringr)
library(tidyr)
# get new column names
ids <- unlist(strsplit(names(df)[-1],
split = ".",
fixed = TRUE))
# get name of column to split
split_column <- names(df)[-1]
df %>%
separate(split_column, into = ids, extra = "merge")
This works within the script file I am using, but when i source the script I get the following error:
Error: `var` must evaluate to a single number or a column name, not a character vector
Why does this work when I run it like normal in RStudio, but when I source the script it throws this error?
Also, is this the optimal way of actually splitting a column of unknown name into new columns with unknown names?
I source the script with the following code, in another script file:
system(paste("Rscript script.R", opt$m, opt$o))
Where opt$m and opt$o are directory paths. This works fine with a similar script I have, but with the script above it throws an error.
I was hoping for some kind of function like separate_at, but that doesn't exist as of yet..
r dplyr tidyr
I have a data frame like this:
structure(list(header = 1:10, ST.adk.fumC.gyrB.icd.mdh.purA.recA = c(" 10 10 11 4 8 8 8 2",
" 48 6 11 4 8 8 8 2", " 58 6 4 4 16 24 8 14", " 88* 6* 4 12 1 20 12 7",
" 117 20 45 41 43 5 32 2", " 7036 526 7 1 1 8 71 6", " 101 43 41 15 18 11 7 6",
" 3595 112 11 5 12 8 88 86", " 117 20 45 41 43 5 32 2", " 744 10 11 135 8 8 8 2"
)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))
What I want to do is to split the second column into separate columns, separated by the "." in the column name. However, it's not always known what the name of the column is, which is why I cannot use the name of the column in dplyr' "separate" function.
I tried the following:
library(dplyr)
library(stringr)
library(tidyr)
# get new column names
ids <- unlist(strsplit(names(df)[-1],
split = ".",
fixed = TRUE))
# get name of column to split
split_column <- names(df)[-1]
df %>%
separate(split_column, into = ids, extra = "merge")
This works within the script file I am using, but when i source the script I get the following error:
Error: `var` must evaluate to a single number or a column name, not a character vector
Why does this work when I run it like normal in RStudio, but when I source the script it throws this error?
Also, is this the optimal way of actually splitting a column of unknown name into new columns with unknown names?
I source the script with the following code, in another script file:
system(paste("Rscript script.R", opt$m, opt$o))
Where opt$m and opt$o are directory paths. This works fine with a similar script I have, but with the script above it throws an error.
I was hoping for some kind of function like separate_at, but that doesn't exist as of yet..
r dplyr tidyr
r dplyr tidyr
edited Nov 24 '18 at 11:08
jay.sf
4,83021539
4,83021539
asked Nov 23 '18 at 12:43
HaakonkasHaakonkas
390111
390111
Works fine for me: R version 3.4.2, tidyr_0.8.1 dplyr_0.7.7
– zx8754
Nov 23 '18 at 12:54
You might have different versions of R or packages, when using Rstudio vs using sourcing the script.
– zx8754
Nov 23 '18 at 12:55
@zx8754 yes I think that must be the case - I will check out the versions
– Haakonkas
Nov 23 '18 at 12:59
Cannot reproduce. Two things: you might want to run your split column through trimws() before separating, otherwise your first column will be blank with the data you gave. Second - consider callingtidyr::separate()
, as your script maybe accessing a different package for some reason.
– iod
Nov 23 '18 at 13:00
(also, separate is fromtidyr
, notdplyr
)
– iod
Nov 23 '18 at 13:01
|
show 5 more comments
Works fine for me: R version 3.4.2, tidyr_0.8.1 dplyr_0.7.7
– zx8754
Nov 23 '18 at 12:54
You might have different versions of R or packages, when using Rstudio vs using sourcing the script.
– zx8754
Nov 23 '18 at 12:55
@zx8754 yes I think that must be the case - I will check out the versions
– Haakonkas
Nov 23 '18 at 12:59
Cannot reproduce. Two things: you might want to run your split column through trimws() before separating, otherwise your first column will be blank with the data you gave. Second - consider callingtidyr::separate()
, as your script maybe accessing a different package for some reason.
– iod
Nov 23 '18 at 13:00
(also, separate is fromtidyr
, notdplyr
)
– iod
Nov 23 '18 at 13:01
Works fine for me: R version 3.4.2, tidyr_0.8.1 dplyr_0.7.7
– zx8754
Nov 23 '18 at 12:54
Works fine for me: R version 3.4.2, tidyr_0.8.1 dplyr_0.7.7
– zx8754
Nov 23 '18 at 12:54
You might have different versions of R or packages, when using Rstudio vs using sourcing the script.
– zx8754
Nov 23 '18 at 12:55
You might have different versions of R or packages, when using Rstudio vs using sourcing the script.
– zx8754
Nov 23 '18 at 12:55
@zx8754 yes I think that must be the case - I will check out the versions
– Haakonkas
Nov 23 '18 at 12:59
@zx8754 yes I think that must be the case - I will check out the versions
– Haakonkas
Nov 23 '18 at 12:59
Cannot reproduce. Two things: you might want to run your split column through trimws() before separating, otherwise your first column will be blank with the data you gave. Second - consider calling
tidyr::separate()
, as your script maybe accessing a different package for some reason.– iod
Nov 23 '18 at 13:00
Cannot reproduce. Two things: you might want to run your split column through trimws() before separating, otherwise your first column will be blank with the data you gave. Second - consider calling
tidyr::separate()
, as your script maybe accessing a different package for some reason.– iod
Nov 23 '18 at 13:00
(also, separate is from
tidyr
, not dplyr
)– iod
Nov 23 '18 at 13:01
(also, separate is from
tidyr
, not dplyr
)– iod
Nov 23 '18 at 13:01
|
show 5 more comments
2 Answers
2
active
oldest
votes
Pretty much the same solution as your example with a few tweaks. This is how I would do it, assuming you want to remove the '*'
in the columns:
library(tidyverse)
library(hablar)
# Vector of new column names
ids <- simplify(strsplit(names(df)[-1],
split = ".",
fixed = T))
# Seperate second column
df %>%
mutate_at(2, funs(trimws(gsub("\*", "", .)))) %>%
separate(2, into = ids, extra = "merge", sep = " ") %>%
retype()
gives you:
# A tibble: 10 x 9
header ST adk fumC gyrB icd mdh purA recA
<int> <int> <int> <int> <int> <int> <int> <int> <int>
1 1 10 10 11 4 8 8 8 2
2 2 48 6 11 4 8 8 8 2
3 3 58 6 4 4 16 24 8 14
4 4 88 6 4 12 1 20 12 7
5 5 117 20 45 41 43 5 32 2
6 6 7036 526 7 1 1 8 71 6
7 7 101 43 41 15 18 11 7 6
8 8 3595 112 11 5 12 8 88 86
9 9 117 20 45 41 43 5 32 2
10 10 744 10 11 135 8 8 8 2
add a comment |
You could use strsplit()
.
split <- do.call(rbind, strsplit(gsub("\*", "", df[, -1]), " "))[, -1]
df1 <- data.frame(df[, 1], split)
df1 <- lapply(df1, function(x) as.numeric(as.character(x)))
names(df1) <- unlist(strsplit(names(df), split = ".", fixed=TRUE))
> df1
header ST adk fumC gyrB icd mdh purA recA
1 1 10 10 11 4 8 8 8 2
2 2 48 6 11 4 8 8 8 2
3 3 58 6 4 4 16 24 8 14
4 4 88 6 4 12 1 20 12 7
5 5 117 20 45 41 43 5 32 2
6 6 7036 526 7 1 1 8 71 6
7 7 101 43 41 15 18 11 7 6
8 8 3595 112 11 5 12 8 88 86
9 9 117 20 45 41 43 5 32 2
10 10 744 10 11 135 8 8 8 2
Data
df <-structure(list(header = 1:10, ST.adk.fumC.gyrB.icd.mdh.purA.recA = c(" 10 10 11 4 8 8 8 2",
" 48 6 11 4 8 8 8 2", " 58 6 4 4 16 24 8 14", " 88* 6* 4 12 1 20 12 7",
" 117 20 45 41 43 5 32 2", " 7036 526 7 1 1 8 71 6", " 101 43 41 15 18 11 7 6",
" 3595 112 11 5 12 8 88 86", " 117 20 45 41 43 5 32 2", " 744 10 11 135 8 8 8 2"
)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))
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%2f53446945%2fseparate-column-with-unknown-name%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
Pretty much the same solution as your example with a few tweaks. This is how I would do it, assuming you want to remove the '*'
in the columns:
library(tidyverse)
library(hablar)
# Vector of new column names
ids <- simplify(strsplit(names(df)[-1],
split = ".",
fixed = T))
# Seperate second column
df %>%
mutate_at(2, funs(trimws(gsub("\*", "", .)))) %>%
separate(2, into = ids, extra = "merge", sep = " ") %>%
retype()
gives you:
# A tibble: 10 x 9
header ST adk fumC gyrB icd mdh purA recA
<int> <int> <int> <int> <int> <int> <int> <int> <int>
1 1 10 10 11 4 8 8 8 2
2 2 48 6 11 4 8 8 8 2
3 3 58 6 4 4 16 24 8 14
4 4 88 6 4 12 1 20 12 7
5 5 117 20 45 41 43 5 32 2
6 6 7036 526 7 1 1 8 71 6
7 7 101 43 41 15 18 11 7 6
8 8 3595 112 11 5 12 8 88 86
9 9 117 20 45 41 43 5 32 2
10 10 744 10 11 135 8 8 8 2
add a comment |
Pretty much the same solution as your example with a few tweaks. This is how I would do it, assuming you want to remove the '*'
in the columns:
library(tidyverse)
library(hablar)
# Vector of new column names
ids <- simplify(strsplit(names(df)[-1],
split = ".",
fixed = T))
# Seperate second column
df %>%
mutate_at(2, funs(trimws(gsub("\*", "", .)))) %>%
separate(2, into = ids, extra = "merge", sep = " ") %>%
retype()
gives you:
# A tibble: 10 x 9
header ST adk fumC gyrB icd mdh purA recA
<int> <int> <int> <int> <int> <int> <int> <int> <int>
1 1 10 10 11 4 8 8 8 2
2 2 48 6 11 4 8 8 8 2
3 3 58 6 4 4 16 24 8 14
4 4 88 6 4 12 1 20 12 7
5 5 117 20 45 41 43 5 32 2
6 6 7036 526 7 1 1 8 71 6
7 7 101 43 41 15 18 11 7 6
8 8 3595 112 11 5 12 8 88 86
9 9 117 20 45 41 43 5 32 2
10 10 744 10 11 135 8 8 8 2
add a comment |
Pretty much the same solution as your example with a few tweaks. This is how I would do it, assuming you want to remove the '*'
in the columns:
library(tidyverse)
library(hablar)
# Vector of new column names
ids <- simplify(strsplit(names(df)[-1],
split = ".",
fixed = T))
# Seperate second column
df %>%
mutate_at(2, funs(trimws(gsub("\*", "", .)))) %>%
separate(2, into = ids, extra = "merge", sep = " ") %>%
retype()
gives you:
# A tibble: 10 x 9
header ST adk fumC gyrB icd mdh purA recA
<int> <int> <int> <int> <int> <int> <int> <int> <int>
1 1 10 10 11 4 8 8 8 2
2 2 48 6 11 4 8 8 8 2
3 3 58 6 4 4 16 24 8 14
4 4 88 6 4 12 1 20 12 7
5 5 117 20 45 41 43 5 32 2
6 6 7036 526 7 1 1 8 71 6
7 7 101 43 41 15 18 11 7 6
8 8 3595 112 11 5 12 8 88 86
9 9 117 20 45 41 43 5 32 2
10 10 744 10 11 135 8 8 8 2
Pretty much the same solution as your example with a few tweaks. This is how I would do it, assuming you want to remove the '*'
in the columns:
library(tidyverse)
library(hablar)
# Vector of new column names
ids <- simplify(strsplit(names(df)[-1],
split = ".",
fixed = T))
# Seperate second column
df %>%
mutate_at(2, funs(trimws(gsub("\*", "", .)))) %>%
separate(2, into = ids, extra = "merge", sep = " ") %>%
retype()
gives you:
# A tibble: 10 x 9
header ST adk fumC gyrB icd mdh purA recA
<int> <int> <int> <int> <int> <int> <int> <int> <int>
1 1 10 10 11 4 8 8 8 2
2 2 48 6 11 4 8 8 8 2
3 3 58 6 4 4 16 24 8 14
4 4 88 6 4 12 1 20 12 7
5 5 117 20 45 41 43 5 32 2
6 6 7036 526 7 1 1 8 71 6
7 7 101 43 41 15 18 11 7 6
8 8 3595 112 11 5 12 8 88 86
9 9 117 20 45 41 43 5 32 2
10 10 744 10 11 135 8 8 8 2
edited Nov 24 '18 at 11:08
answered Nov 24 '18 at 10:56
davsjobdavsjob
57236
57236
add a comment |
add a comment |
You could use strsplit()
.
split <- do.call(rbind, strsplit(gsub("\*", "", df[, -1]), " "))[, -1]
df1 <- data.frame(df[, 1], split)
df1 <- lapply(df1, function(x) as.numeric(as.character(x)))
names(df1) <- unlist(strsplit(names(df), split = ".", fixed=TRUE))
> df1
header ST adk fumC gyrB icd mdh purA recA
1 1 10 10 11 4 8 8 8 2
2 2 48 6 11 4 8 8 8 2
3 3 58 6 4 4 16 24 8 14
4 4 88 6 4 12 1 20 12 7
5 5 117 20 45 41 43 5 32 2
6 6 7036 526 7 1 1 8 71 6
7 7 101 43 41 15 18 11 7 6
8 8 3595 112 11 5 12 8 88 86
9 9 117 20 45 41 43 5 32 2
10 10 744 10 11 135 8 8 8 2
Data
df <-structure(list(header = 1:10, ST.adk.fumC.gyrB.icd.mdh.purA.recA = c(" 10 10 11 4 8 8 8 2",
" 48 6 11 4 8 8 8 2", " 58 6 4 4 16 24 8 14", " 88* 6* 4 12 1 20 12 7",
" 117 20 45 41 43 5 32 2", " 7036 526 7 1 1 8 71 6", " 101 43 41 15 18 11 7 6",
" 3595 112 11 5 12 8 88 86", " 117 20 45 41 43 5 32 2", " 744 10 11 135 8 8 8 2"
)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))
add a comment |
You could use strsplit()
.
split <- do.call(rbind, strsplit(gsub("\*", "", df[, -1]), " "))[, -1]
df1 <- data.frame(df[, 1], split)
df1 <- lapply(df1, function(x) as.numeric(as.character(x)))
names(df1) <- unlist(strsplit(names(df), split = ".", fixed=TRUE))
> df1
header ST adk fumC gyrB icd mdh purA recA
1 1 10 10 11 4 8 8 8 2
2 2 48 6 11 4 8 8 8 2
3 3 58 6 4 4 16 24 8 14
4 4 88 6 4 12 1 20 12 7
5 5 117 20 45 41 43 5 32 2
6 6 7036 526 7 1 1 8 71 6
7 7 101 43 41 15 18 11 7 6
8 8 3595 112 11 5 12 8 88 86
9 9 117 20 45 41 43 5 32 2
10 10 744 10 11 135 8 8 8 2
Data
df <-structure(list(header = 1:10, ST.adk.fumC.gyrB.icd.mdh.purA.recA = c(" 10 10 11 4 8 8 8 2",
" 48 6 11 4 8 8 8 2", " 58 6 4 4 16 24 8 14", " 88* 6* 4 12 1 20 12 7",
" 117 20 45 41 43 5 32 2", " 7036 526 7 1 1 8 71 6", " 101 43 41 15 18 11 7 6",
" 3595 112 11 5 12 8 88 86", " 117 20 45 41 43 5 32 2", " 744 10 11 135 8 8 8 2"
)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))
add a comment |
You could use strsplit()
.
split <- do.call(rbind, strsplit(gsub("\*", "", df[, -1]), " "))[, -1]
df1 <- data.frame(df[, 1], split)
df1 <- lapply(df1, function(x) as.numeric(as.character(x)))
names(df1) <- unlist(strsplit(names(df), split = ".", fixed=TRUE))
> df1
header ST adk fumC gyrB icd mdh purA recA
1 1 10 10 11 4 8 8 8 2
2 2 48 6 11 4 8 8 8 2
3 3 58 6 4 4 16 24 8 14
4 4 88 6 4 12 1 20 12 7
5 5 117 20 45 41 43 5 32 2
6 6 7036 526 7 1 1 8 71 6
7 7 101 43 41 15 18 11 7 6
8 8 3595 112 11 5 12 8 88 86
9 9 117 20 45 41 43 5 32 2
10 10 744 10 11 135 8 8 8 2
Data
df <-structure(list(header = 1:10, ST.adk.fumC.gyrB.icd.mdh.purA.recA = c(" 10 10 11 4 8 8 8 2",
" 48 6 11 4 8 8 8 2", " 58 6 4 4 16 24 8 14", " 88* 6* 4 12 1 20 12 7",
" 117 20 45 41 43 5 32 2", " 7036 526 7 1 1 8 71 6", " 101 43 41 15 18 11 7 6",
" 3595 112 11 5 12 8 88 86", " 117 20 45 41 43 5 32 2", " 744 10 11 135 8 8 8 2"
)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))
You could use strsplit()
.
split <- do.call(rbind, strsplit(gsub("\*", "", df[, -1]), " "))[, -1]
df1 <- data.frame(df[, 1], split)
df1 <- lapply(df1, function(x) as.numeric(as.character(x)))
names(df1) <- unlist(strsplit(names(df), split = ".", fixed=TRUE))
> df1
header ST adk fumC gyrB icd mdh purA recA
1 1 10 10 11 4 8 8 8 2
2 2 48 6 11 4 8 8 8 2
3 3 58 6 4 4 16 24 8 14
4 4 88 6 4 12 1 20 12 7
5 5 117 20 45 41 43 5 32 2
6 6 7036 526 7 1 1 8 71 6
7 7 101 43 41 15 18 11 7 6
8 8 3595 112 11 5 12 8 88 86
9 9 117 20 45 41 43 5 32 2
10 10 744 10 11 135 8 8 8 2
Data
df <-structure(list(header = 1:10, ST.adk.fumC.gyrB.icd.mdh.purA.recA = c(" 10 10 11 4 8 8 8 2",
" 48 6 11 4 8 8 8 2", " 58 6 4 4 16 24 8 14", " 88* 6* 4 12 1 20 12 7",
" 117 20 45 41 43 5 32 2", " 7036 526 7 1 1 8 71 6", " 101 43 41 15 18 11 7 6",
" 3595 112 11 5 12 8 88 86", " 117 20 45 41 43 5 32 2", " 744 10 11 135 8 8 8 2"
)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))
edited Nov 25 '18 at 8:40
answered Nov 24 '18 at 11:05
jay.sfjay.sf
4,83021539
4,83021539
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%2f53446945%2fseparate-column-with-unknown-name%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
Works fine for me: R version 3.4.2, tidyr_0.8.1 dplyr_0.7.7
– zx8754
Nov 23 '18 at 12:54
You might have different versions of R or packages, when using Rstudio vs using sourcing the script.
– zx8754
Nov 23 '18 at 12:55
@zx8754 yes I think that must be the case - I will check out the versions
– Haakonkas
Nov 23 '18 at 12:59
Cannot reproduce. Two things: you might want to run your split column through trimws() before separating, otherwise your first column will be blank with the data you gave. Second - consider calling
tidyr::separate()
, as your script maybe accessing a different package for some reason.– iod
Nov 23 '18 at 13:00
(also, separate is from
tidyr
, notdplyr
)– iod
Nov 23 '18 at 13:01