replacing special characters in first column only in r
Hi I have a text file in this format:
x
M.00116 952
M.00046 41483
M.00033 4
I need to replace the "." with an "_" in r. But for I am not able to do it by using this :
sub("\.", "_", c)
I get this output
c(952, 41483, 4)
I need an out put like :
x
M-00116 952
M-00046 41483
M-00033 4
What am I doing wrong? Any help is appreciated!
r replace
add a comment |
Hi I have a text file in this format:
x
M.00116 952
M.00046 41483
M.00033 4
I need to replace the "." with an "_" in r. But for I am not able to do it by using this :
sub("\.", "_", c)
I get this output
c(952, 41483, 4)
I need an out put like :
x
M-00116 952
M-00046 41483
M-00033 4
What am I doing wrong? Any help is appreciated!
r replace
add a comment |
Hi I have a text file in this format:
x
M.00116 952
M.00046 41483
M.00033 4
I need to replace the "." with an "_" in r. But for I am not able to do it by using this :
sub("\.", "_", c)
I get this output
c(952, 41483, 4)
I need an out put like :
x
M-00116 952
M-00046 41483
M-00033 4
What am I doing wrong? Any help is appreciated!
r replace
Hi I have a text file in this format:
x
M.00116 952
M.00046 41483
M.00033 4
I need to replace the "." with an "_" in r. But for I am not able to do it by using this :
sub("\.", "_", c)
I get this output
c(952, 41483, 4)
I need an out put like :
x
M-00116 952
M-00046 41483
M-00033 4
What am I doing wrong? Any help is appreciated!
r replace
r replace
edited Nov 14 '18 at 16:44
Jake Kaupp
5,64721428
5,64721428
asked Nov 14 '18 at 16:40
R_explorerR_explorer
104
104
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
We can use chartr from base R
chartr('.', '-', x)
#[1] "M-00116 952 M-00046 41483 M-00033 4"
data
x <- "M.00116 952 M.00046 41483 M.00033 4"
tried this as well, but does not seem to work. My data frame c is in two columns. First column has no header and has "M.XXXX" and second column is a number. sorry for the typo in my original post, the "x" is the header for col 2 not col 1. Maybe that is why? I can paste a header to my first column and force it to do it, but I don't want to do that unnecessary step! can't see what is troubling here, should be quite simple!
– R_explorer
Nov 14 '18 at 16:57
@user2635253 Looks like it isrownames.tryrow.names(yourdata) <- chartr('.', '-', row.names(yourdata))
– akrun
Nov 14 '18 at 16:58
1
Awesome! That did it! I was looking at it as a col of M.XXXX I should have looked at it as row names. Thanks so much for your help! This worked!
– R_explorer
Nov 14 '18 at 17:00
add a comment |
Try:
x <- "M.00116 952 M.00046 41483 M.00033 4"
gsub("\.", "-", x)
EDIT:
Replace "sub" with gsub:
gsub("\.", "_", data$colname)
EDIT:
This worked for me:
c <- c("M.00116", "M.00046", "M.00033")
x <- c("952", "41483", "4")
d <- cbind(c, x)
colnames(d)[2] <- ""
gsub("\.", "_", d)
c
[1,] "M_00116" "952"
[2,] "M_00046" "41483"
[3,] "M_00033" "4"
I have it this way: > head(c) x M.00116 952 M.00046 41483 M.00033 4 M.00142 0 M.00048 0 M.00113 30347 I need to replace the "." only in my first column not my second
– R_explorer
Nov 14 '18 at 16:45
Are you specifying the column name or usingsubover the whole data frame?
– user113156
Nov 14 '18 at 16:48
tried did not work like the way I want it: I get this output > gsub("\.", "-", c) [1] "c(952, 41483, 4) does not do it
– R_explorer
Nov 14 '18 at 16:49
Change it to:gsub("\.", "-", data$column_name)
– user113156
Nov 14 '18 at 16:49
I am using it over the whole data frame. the first column has no column name
– R_explorer
Nov 14 '18 at 16:49
|
show 2 more comments
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%2f53304957%2freplacing-special-characters-in-first-column-only-in-r%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 can use chartr from base R
chartr('.', '-', x)
#[1] "M-00116 952 M-00046 41483 M-00033 4"
data
x <- "M.00116 952 M.00046 41483 M.00033 4"
tried this as well, but does not seem to work. My data frame c is in two columns. First column has no header and has "M.XXXX" and second column is a number. sorry for the typo in my original post, the "x" is the header for col 2 not col 1. Maybe that is why? I can paste a header to my first column and force it to do it, but I don't want to do that unnecessary step! can't see what is troubling here, should be quite simple!
– R_explorer
Nov 14 '18 at 16:57
@user2635253 Looks like it isrownames.tryrow.names(yourdata) <- chartr('.', '-', row.names(yourdata))
– akrun
Nov 14 '18 at 16:58
1
Awesome! That did it! I was looking at it as a col of M.XXXX I should have looked at it as row names. Thanks so much for your help! This worked!
– R_explorer
Nov 14 '18 at 17:00
add a comment |
We can use chartr from base R
chartr('.', '-', x)
#[1] "M-00116 952 M-00046 41483 M-00033 4"
data
x <- "M.00116 952 M.00046 41483 M.00033 4"
tried this as well, but does not seem to work. My data frame c is in two columns. First column has no header and has "M.XXXX" and second column is a number. sorry for the typo in my original post, the "x" is the header for col 2 not col 1. Maybe that is why? I can paste a header to my first column and force it to do it, but I don't want to do that unnecessary step! can't see what is troubling here, should be quite simple!
– R_explorer
Nov 14 '18 at 16:57
@user2635253 Looks like it isrownames.tryrow.names(yourdata) <- chartr('.', '-', row.names(yourdata))
– akrun
Nov 14 '18 at 16:58
1
Awesome! That did it! I was looking at it as a col of M.XXXX I should have looked at it as row names. Thanks so much for your help! This worked!
– R_explorer
Nov 14 '18 at 17:00
add a comment |
We can use chartr from base R
chartr('.', '-', x)
#[1] "M-00116 952 M-00046 41483 M-00033 4"
data
x <- "M.00116 952 M.00046 41483 M.00033 4"
We can use chartr from base R
chartr('.', '-', x)
#[1] "M-00116 952 M-00046 41483 M-00033 4"
data
x <- "M.00116 952 M.00046 41483 M.00033 4"
answered Nov 14 '18 at 16:43
akrunakrun
408k13198273
408k13198273
tried this as well, but does not seem to work. My data frame c is in two columns. First column has no header and has "M.XXXX" and second column is a number. sorry for the typo in my original post, the "x" is the header for col 2 not col 1. Maybe that is why? I can paste a header to my first column and force it to do it, but I don't want to do that unnecessary step! can't see what is troubling here, should be quite simple!
– R_explorer
Nov 14 '18 at 16:57
@user2635253 Looks like it isrownames.tryrow.names(yourdata) <- chartr('.', '-', row.names(yourdata))
– akrun
Nov 14 '18 at 16:58
1
Awesome! That did it! I was looking at it as a col of M.XXXX I should have looked at it as row names. Thanks so much for your help! This worked!
– R_explorer
Nov 14 '18 at 17:00
add a comment |
tried this as well, but does not seem to work. My data frame c is in two columns. First column has no header and has "M.XXXX" and second column is a number. sorry for the typo in my original post, the "x" is the header for col 2 not col 1. Maybe that is why? I can paste a header to my first column and force it to do it, but I don't want to do that unnecessary step! can't see what is troubling here, should be quite simple!
– R_explorer
Nov 14 '18 at 16:57
@user2635253 Looks like it isrownames.tryrow.names(yourdata) <- chartr('.', '-', row.names(yourdata))
– akrun
Nov 14 '18 at 16:58
1
Awesome! That did it! I was looking at it as a col of M.XXXX I should have looked at it as row names. Thanks so much for your help! This worked!
– R_explorer
Nov 14 '18 at 17:00
tried this as well, but does not seem to work. My data frame c is in two columns. First column has no header and has "M.XXXX" and second column is a number. sorry for the typo in my original post, the "x" is the header for col 2 not col 1. Maybe that is why? I can paste a header to my first column and force it to do it, but I don't want to do that unnecessary step! can't see what is troubling here, should be quite simple!
– R_explorer
Nov 14 '18 at 16:57
tried this as well, but does not seem to work. My data frame c is in two columns. First column has no header and has "M.XXXX" and second column is a number. sorry for the typo in my original post, the "x" is the header for col 2 not col 1. Maybe that is why? I can paste a header to my first column and force it to do it, but I don't want to do that unnecessary step! can't see what is troubling here, should be quite simple!
– R_explorer
Nov 14 '18 at 16:57
@user2635253 Looks like it is
rownames. try row.names(yourdata) <- chartr('.', '-', row.names(yourdata))– akrun
Nov 14 '18 at 16:58
@user2635253 Looks like it is
rownames. try row.names(yourdata) <- chartr('.', '-', row.names(yourdata))– akrun
Nov 14 '18 at 16:58
1
1
Awesome! That did it! I was looking at it as a col of M.XXXX I should have looked at it as row names. Thanks so much for your help! This worked!
– R_explorer
Nov 14 '18 at 17:00
Awesome! That did it! I was looking at it as a col of M.XXXX I should have looked at it as row names. Thanks so much for your help! This worked!
– R_explorer
Nov 14 '18 at 17:00
add a comment |
Try:
x <- "M.00116 952 M.00046 41483 M.00033 4"
gsub("\.", "-", x)
EDIT:
Replace "sub" with gsub:
gsub("\.", "_", data$colname)
EDIT:
This worked for me:
c <- c("M.00116", "M.00046", "M.00033")
x <- c("952", "41483", "4")
d <- cbind(c, x)
colnames(d)[2] <- ""
gsub("\.", "_", d)
c
[1,] "M_00116" "952"
[2,] "M_00046" "41483"
[3,] "M_00033" "4"
I have it this way: > head(c) x M.00116 952 M.00046 41483 M.00033 4 M.00142 0 M.00048 0 M.00113 30347 I need to replace the "." only in my first column not my second
– R_explorer
Nov 14 '18 at 16:45
Are you specifying the column name or usingsubover the whole data frame?
– user113156
Nov 14 '18 at 16:48
tried did not work like the way I want it: I get this output > gsub("\.", "-", c) [1] "c(952, 41483, 4) does not do it
– R_explorer
Nov 14 '18 at 16:49
Change it to:gsub("\.", "-", data$column_name)
– user113156
Nov 14 '18 at 16:49
I am using it over the whole data frame. the first column has no column name
– R_explorer
Nov 14 '18 at 16:49
|
show 2 more comments
Try:
x <- "M.00116 952 M.00046 41483 M.00033 4"
gsub("\.", "-", x)
EDIT:
Replace "sub" with gsub:
gsub("\.", "_", data$colname)
EDIT:
This worked for me:
c <- c("M.00116", "M.00046", "M.00033")
x <- c("952", "41483", "4")
d <- cbind(c, x)
colnames(d)[2] <- ""
gsub("\.", "_", d)
c
[1,] "M_00116" "952"
[2,] "M_00046" "41483"
[3,] "M_00033" "4"
I have it this way: > head(c) x M.00116 952 M.00046 41483 M.00033 4 M.00142 0 M.00048 0 M.00113 30347 I need to replace the "." only in my first column not my second
– R_explorer
Nov 14 '18 at 16:45
Are you specifying the column name or usingsubover the whole data frame?
– user113156
Nov 14 '18 at 16:48
tried did not work like the way I want it: I get this output > gsub("\.", "-", c) [1] "c(952, 41483, 4) does not do it
– R_explorer
Nov 14 '18 at 16:49
Change it to:gsub("\.", "-", data$column_name)
– user113156
Nov 14 '18 at 16:49
I am using it over the whole data frame. the first column has no column name
– R_explorer
Nov 14 '18 at 16:49
|
show 2 more comments
Try:
x <- "M.00116 952 M.00046 41483 M.00033 4"
gsub("\.", "-", x)
EDIT:
Replace "sub" with gsub:
gsub("\.", "_", data$colname)
EDIT:
This worked for me:
c <- c("M.00116", "M.00046", "M.00033")
x <- c("952", "41483", "4")
d <- cbind(c, x)
colnames(d)[2] <- ""
gsub("\.", "_", d)
c
[1,] "M_00116" "952"
[2,] "M_00046" "41483"
[3,] "M_00033" "4"
Try:
x <- "M.00116 952 M.00046 41483 M.00033 4"
gsub("\.", "-", x)
EDIT:
Replace "sub" with gsub:
gsub("\.", "_", data$colname)
EDIT:
This worked for me:
c <- c("M.00116", "M.00046", "M.00033")
x <- c("952", "41483", "4")
d <- cbind(c, x)
colnames(d)[2] <- ""
gsub("\.", "_", d)
c
[1,] "M_00116" "952"
[2,] "M_00046" "41483"
[3,] "M_00033" "4"
edited Nov 14 '18 at 16:55
answered Nov 14 '18 at 16:42
user113156user113156
8871419
8871419
I have it this way: > head(c) x M.00116 952 M.00046 41483 M.00033 4 M.00142 0 M.00048 0 M.00113 30347 I need to replace the "." only in my first column not my second
– R_explorer
Nov 14 '18 at 16:45
Are you specifying the column name or usingsubover the whole data frame?
– user113156
Nov 14 '18 at 16:48
tried did not work like the way I want it: I get this output > gsub("\.", "-", c) [1] "c(952, 41483, 4) does not do it
– R_explorer
Nov 14 '18 at 16:49
Change it to:gsub("\.", "-", data$column_name)
– user113156
Nov 14 '18 at 16:49
I am using it over the whole data frame. the first column has no column name
– R_explorer
Nov 14 '18 at 16:49
|
show 2 more comments
I have it this way: > head(c) x M.00116 952 M.00046 41483 M.00033 4 M.00142 0 M.00048 0 M.00113 30347 I need to replace the "." only in my first column not my second
– R_explorer
Nov 14 '18 at 16:45
Are you specifying the column name or usingsubover the whole data frame?
– user113156
Nov 14 '18 at 16:48
tried did not work like the way I want it: I get this output > gsub("\.", "-", c) [1] "c(952, 41483, 4) does not do it
– R_explorer
Nov 14 '18 at 16:49
Change it to:gsub("\.", "-", data$column_name)
– user113156
Nov 14 '18 at 16:49
I am using it over the whole data frame. the first column has no column name
– R_explorer
Nov 14 '18 at 16:49
I have it this way: > head(c) x M.00116 952 M.00046 41483 M.00033 4 M.00142 0 M.00048 0 M.00113 30347 I need to replace the "." only in my first column not my second
– R_explorer
Nov 14 '18 at 16:45
I have it this way: > head(c) x M.00116 952 M.00046 41483 M.00033 4 M.00142 0 M.00048 0 M.00113 30347 I need to replace the "." only in my first column not my second
– R_explorer
Nov 14 '18 at 16:45
Are you specifying the column name or using
sub over the whole data frame?– user113156
Nov 14 '18 at 16:48
Are you specifying the column name or using
sub over the whole data frame?– user113156
Nov 14 '18 at 16:48
tried did not work like the way I want it: I get this output > gsub("\.", "-", c) [1] "c(952, 41483, 4) does not do it
– R_explorer
Nov 14 '18 at 16:49
tried did not work like the way I want it: I get this output > gsub("\.", "-", c) [1] "c(952, 41483, 4) does not do it
– R_explorer
Nov 14 '18 at 16:49
Change it to:
gsub("\.", "-", data$column_name) – user113156
Nov 14 '18 at 16:49
Change it to:
gsub("\.", "-", data$column_name) – user113156
Nov 14 '18 at 16:49
I am using it over the whole data frame. the first column has no column name
– R_explorer
Nov 14 '18 at 16:49
I am using it over the whole data frame. the first column has no column name
– R_explorer
Nov 14 '18 at 16:49
|
show 2 more comments
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%2f53304957%2freplacing-special-characters-in-first-column-only-in-r%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