Shifting non-NA cells to the left2019 Community Moderator ElectionHow to move cells with a value row-wise to the left in a dataframeUsing R to shift values to the left of data.frameshift right cell contents in a row by condition (or taking the previous value) in dplyrMove dataframe columns values to the left (based on na)data.table shift right all cell values by number of na within each row [R]Shift cells to left in R data.frameHow to join (merge) data frames (inner, outer, left, right)?Convert data.frame columns from factors to charactersWhy is `[` better than `subset`?How to remove records in a dataframeShift column in pandas dataframe up by one?data.table vs dplyr: can one do something well the other can't or does poorly?How to move cells with a value row-wise to the left in a dataframeAdding Proportionate Na's in a columnDividing across multiple columns in r using mutate_at callCopying Column values in pandas to non zero cells and aggreagating columns after it
Output visual diagram of picture
Jem'Hadar, something strange about their life expectancy
UK Tourist Visa- Enquiry
Does convergence of polynomials imply that of its coefficients?
What is the tangent at a sharp point on a curve?
Nested Dynamic SOQL Query
Should I be concerned about student access to a test bank?
Could any one tell what PN is this Chip? Thanks~
Homology of the fiber
PTIJ: Which Dr. Seuss books should one obtain?
Why I don't get the wanted width of tcbox?
Turning a hard to access nut?
Gauss brackets with double vertical lines
Exposing a company lying about themselves in a tightly knit industry: Is my career at risk on the long run?
Isn't the word "experience" wrongly used in this context?
Do I need to convey a moral for each of my blog post?
What is the difference between something being completely legal and being completely decriminalized?
Is there any common country to visit for uk and schengen visa?
Do I need an EFI partition for each 18.04 ubuntu I have on my HD?
Norwegian Refugee travel document
What (if any) is the reason to buy in small local stores?
What is 管理しきれず?
Is xar preinstalled on macOS?
How old is Nick Fury?
Shifting non-NA cells to the left
2019 Community Moderator ElectionHow to move cells with a value row-wise to the left in a dataframeUsing R to shift values to the left of data.frameshift right cell contents in a row by condition (or taking the previous value) in dplyrMove dataframe columns values to the left (based on na)data.table shift right all cell values by number of na within each row [R]Shift cells to left in R data.frameHow to join (merge) data frames (inner, outer, left, right)?Convert data.frame columns from factors to charactersWhy is `[` better than `subset`?How to remove records in a dataframeShift column in pandas dataframe up by one?data.table vs dplyr: can one do something well the other can't or does poorly?How to move cells with a value row-wise to the left in a dataframeAdding Proportionate Na's in a columnDividing across multiple columns in r using mutate_at callCopying Column values in pandas to non zero cells and aggreagating columns after it
There are many NA's in my dataset and I need to shift all those cells (at row level) to the left.
Example- my dataframe:
df=data.frame(x=c("l","m",NA,NA,"p"),y=c(NA,"b","c",NA,NA),z=c("u",NA,"w","x","y"))
df
x y z
1 l <NA> u
2 m b <NA>
3 <NA> c w
4 <NA> <NA> x
5 p <NA> y
I want the above dataframe converted into this:
x y z
1 l u NA
2 m b NA
3 c w NA
4 x <NA> NA
5 p y NA
Please help.
Thanks.
r dataframe data-manipulation
add a comment |
There are many NA's in my dataset and I need to shift all those cells (at row level) to the left.
Example- my dataframe:
df=data.frame(x=c("l","m",NA,NA,"p"),y=c(NA,"b","c",NA,NA),z=c("u",NA,"w","x","y"))
df
x y z
1 l <NA> u
2 m b <NA>
3 <NA> c w
4 <NA> <NA> x
5 p <NA> y
I want the above dataframe converted into this:
x y z
1 l u NA
2 m b NA
3 c w NA
4 x <NA> NA
5 p y NA
Please help.
Thanks.
r dataframe data-manipulation
add a comment |
There are many NA's in my dataset and I need to shift all those cells (at row level) to the left.
Example- my dataframe:
df=data.frame(x=c("l","m",NA,NA,"p"),y=c(NA,"b","c",NA,NA),z=c("u",NA,"w","x","y"))
df
x y z
1 l <NA> u
2 m b <NA>
3 <NA> c w
4 <NA> <NA> x
5 p <NA> y
I want the above dataframe converted into this:
x y z
1 l u NA
2 m b NA
3 c w NA
4 x <NA> NA
5 p y NA
Please help.
Thanks.
r dataframe data-manipulation
There are many NA's in my dataset and I need to shift all those cells (at row level) to the left.
Example- my dataframe:
df=data.frame(x=c("l","m",NA,NA,"p"),y=c(NA,"b","c",NA,NA),z=c("u",NA,"w","x","y"))
df
x y z
1 l <NA> u
2 m b <NA>
3 <NA> c w
4 <NA> <NA> x
5 p <NA> y
I want the above dataframe converted into this:
x y z
1 l u NA
2 m b NA
3 c w NA
4 x <NA> NA
5 p y NA
Please help.
Thanks.
r dataframe data-manipulation
r dataframe data-manipulation
edited Mar 3 '18 at 10:08
Henrik
42.1k994110
42.1k994110
asked Apr 25 '14 at 5:57
sidpatsidpat
361220
361220
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
You can use the standard apply
function:
df=data.frame(x=c("l","m",NA,NA,"p"),y=c(NA,"b","c",NA,NA),z=c("u",NA,"w","x","y"))
df2 = as.data.frame(t(apply(df,1, function(x) return(c(x[!is.na(x)],x[is.na(x)]) ) )))
colnames(df2) = colnames(df)
> df
x y z
1 l <NA> u
2 m b <NA>
3 <NA> c w
4 <NA> <NA> x
5 p <NA> y
> df2
x y z
1 l u <NA>
2 m b <NA>
3 c w <NA>
4 x <NA> <NA>
5 p y <NA>
add a comment |
Thanks to @Richard Scriven for good observation
A) with is.na
and order
, lapply
and rbind
for aggregation
nosort.df<-do.call(rbind,lapply(1:nrow(df),function(x) z=df[x,][order(is.na(df[x,]))];colnames(z)<-c("x","y","z");return(z) ))
> nosort.df
x y z
1 l u <NA>
2 m b <NA>
3 c w <NA>
4 x <NA> <NA>
5 p y <NA>
B) if sorted rows are required:
with sort
, lapply
and rbind
sort.df<-do.call(rbind,lapply(1:nrow(df),function(x) z=sort(df[x,],na.last=TRUE);colnames(z)<-c("x","y","z");return(z) ))
> sort.df
x y z
1 l u <NA>
2 b m <NA>
3 c w <NA>
4 x <NA> <NA>
5 p y <NA>
1
Wait, you're sorting the row? This will change the position of values that don't need to be changed.
– Rich Scriven
Apr 25 '14 at 6:42
add a comment |
If you won't get shorter answer, this should help:
df=data.frame(x=c("l","m",NA,NA,"p"),y=c(NA,"b","c",NA,NA),z=c("u",NA,"w","x","y"))
sapply(df,as.character)
for(i in 1:nrow(df))
sub <- df[i,c(which(!is.na(df[i,])),which(is.na(df[i,])))]
colnames(sub) <- colnames(df)
df[i,] <- sub
The threeas.character
statements could be combined withsapply(df,as.character)
– Silence Dogood
Apr 25 '14 at 7:06
1
thx... I need to start to use those pply functions more.
– Pigeon
Apr 25 '14 at 7:23
add a comment |
If you don't want to use VBA, you can try the following steps.
1. Select your dataset
2. Replace NA will empty cells
3. press F5 and select blanks ok
4. right click on any of the selection and delete (left)
I hope this helps.
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%2f23285215%2fshifting-non-na-cells-to-the-left%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use the standard apply
function:
df=data.frame(x=c("l","m",NA,NA,"p"),y=c(NA,"b","c",NA,NA),z=c("u",NA,"w","x","y"))
df2 = as.data.frame(t(apply(df,1, function(x) return(c(x[!is.na(x)],x[is.na(x)]) ) )))
colnames(df2) = colnames(df)
> df
x y z
1 l <NA> u
2 m b <NA>
3 <NA> c w
4 <NA> <NA> x
5 p <NA> y
> df2
x y z
1 l u <NA>
2 m b <NA>
3 c w <NA>
4 x <NA> <NA>
5 p y <NA>
add a comment |
You can use the standard apply
function:
df=data.frame(x=c("l","m",NA,NA,"p"),y=c(NA,"b","c",NA,NA),z=c("u",NA,"w","x","y"))
df2 = as.data.frame(t(apply(df,1, function(x) return(c(x[!is.na(x)],x[is.na(x)]) ) )))
colnames(df2) = colnames(df)
> df
x y z
1 l <NA> u
2 m b <NA>
3 <NA> c w
4 <NA> <NA> x
5 p <NA> y
> df2
x y z
1 l u <NA>
2 m b <NA>
3 c w <NA>
4 x <NA> <NA>
5 p y <NA>
add a comment |
You can use the standard apply
function:
df=data.frame(x=c("l","m",NA,NA,"p"),y=c(NA,"b","c",NA,NA),z=c("u",NA,"w","x","y"))
df2 = as.data.frame(t(apply(df,1, function(x) return(c(x[!is.na(x)],x[is.na(x)]) ) )))
colnames(df2) = colnames(df)
> df
x y z
1 l <NA> u
2 m b <NA>
3 <NA> c w
4 <NA> <NA> x
5 p <NA> y
> df2
x y z
1 l u <NA>
2 m b <NA>
3 c w <NA>
4 x <NA> <NA>
5 p y <NA>
You can use the standard apply
function:
df=data.frame(x=c("l","m",NA,NA,"p"),y=c(NA,"b","c",NA,NA),z=c("u",NA,"w","x","y"))
df2 = as.data.frame(t(apply(df,1, function(x) return(c(x[!is.na(x)],x[is.na(x)]) ) )))
colnames(df2) = colnames(df)
> df
x y z
1 l <NA> u
2 m b <NA>
3 <NA> c w
4 <NA> <NA> x
5 p <NA> y
> df2
x y z
1 l u <NA>
2 m b <NA>
3 c w <NA>
4 x <NA> <NA>
5 p y <NA>
answered Apr 25 '14 at 6:24
Hans RoggemanHans Roggeman
1,9461128
1,9461128
add a comment |
add a comment |
Thanks to @Richard Scriven for good observation
A) with is.na
and order
, lapply
and rbind
for aggregation
nosort.df<-do.call(rbind,lapply(1:nrow(df),function(x) z=df[x,][order(is.na(df[x,]))];colnames(z)<-c("x","y","z");return(z) ))
> nosort.df
x y z
1 l u <NA>
2 m b <NA>
3 c w <NA>
4 x <NA> <NA>
5 p y <NA>
B) if sorted rows are required:
with sort
, lapply
and rbind
sort.df<-do.call(rbind,lapply(1:nrow(df),function(x) z=sort(df[x,],na.last=TRUE);colnames(z)<-c("x","y","z");return(z) ))
> sort.df
x y z
1 l u <NA>
2 b m <NA>
3 c w <NA>
4 x <NA> <NA>
5 p y <NA>
1
Wait, you're sorting the row? This will change the position of values that don't need to be changed.
– Rich Scriven
Apr 25 '14 at 6:42
add a comment |
Thanks to @Richard Scriven for good observation
A) with is.na
and order
, lapply
and rbind
for aggregation
nosort.df<-do.call(rbind,lapply(1:nrow(df),function(x) z=df[x,][order(is.na(df[x,]))];colnames(z)<-c("x","y","z");return(z) ))
> nosort.df
x y z
1 l u <NA>
2 m b <NA>
3 c w <NA>
4 x <NA> <NA>
5 p y <NA>
B) if sorted rows are required:
with sort
, lapply
and rbind
sort.df<-do.call(rbind,lapply(1:nrow(df),function(x) z=sort(df[x,],na.last=TRUE);colnames(z)<-c("x","y","z");return(z) ))
> sort.df
x y z
1 l u <NA>
2 b m <NA>
3 c w <NA>
4 x <NA> <NA>
5 p y <NA>
1
Wait, you're sorting the row? This will change the position of values that don't need to be changed.
– Rich Scriven
Apr 25 '14 at 6:42
add a comment |
Thanks to @Richard Scriven for good observation
A) with is.na
and order
, lapply
and rbind
for aggregation
nosort.df<-do.call(rbind,lapply(1:nrow(df),function(x) z=df[x,][order(is.na(df[x,]))];colnames(z)<-c("x","y","z");return(z) ))
> nosort.df
x y z
1 l u <NA>
2 m b <NA>
3 c w <NA>
4 x <NA> <NA>
5 p y <NA>
B) if sorted rows are required:
with sort
, lapply
and rbind
sort.df<-do.call(rbind,lapply(1:nrow(df),function(x) z=sort(df[x,],na.last=TRUE);colnames(z)<-c("x","y","z");return(z) ))
> sort.df
x y z
1 l u <NA>
2 b m <NA>
3 c w <NA>
4 x <NA> <NA>
5 p y <NA>
Thanks to @Richard Scriven for good observation
A) with is.na
and order
, lapply
and rbind
for aggregation
nosort.df<-do.call(rbind,lapply(1:nrow(df),function(x) z=df[x,][order(is.na(df[x,]))];colnames(z)<-c("x","y","z");return(z) ))
> nosort.df
x y z
1 l u <NA>
2 m b <NA>
3 c w <NA>
4 x <NA> <NA>
5 p y <NA>
B) if sorted rows are required:
with sort
, lapply
and rbind
sort.df<-do.call(rbind,lapply(1:nrow(df),function(x) z=sort(df[x,],na.last=TRUE);colnames(z)<-c("x","y","z");return(z) ))
> sort.df
x y z
1 l u <NA>
2 b m <NA>
3 c w <NA>
4 x <NA> <NA>
5 p y <NA>
edited Apr 25 '14 at 7:04
answered Apr 25 '14 at 6:33
Silence DogoodSilence Dogood
3,1561815
3,1561815
1
Wait, you're sorting the row? This will change the position of values that don't need to be changed.
– Rich Scriven
Apr 25 '14 at 6:42
add a comment |
1
Wait, you're sorting the row? This will change the position of values that don't need to be changed.
– Rich Scriven
Apr 25 '14 at 6:42
1
1
Wait, you're sorting the row? This will change the position of values that don't need to be changed.
– Rich Scriven
Apr 25 '14 at 6:42
Wait, you're sorting the row? This will change the position of values that don't need to be changed.
– Rich Scriven
Apr 25 '14 at 6:42
add a comment |
If you won't get shorter answer, this should help:
df=data.frame(x=c("l","m",NA,NA,"p"),y=c(NA,"b","c",NA,NA),z=c("u",NA,"w","x","y"))
sapply(df,as.character)
for(i in 1:nrow(df))
sub <- df[i,c(which(!is.na(df[i,])),which(is.na(df[i,])))]
colnames(sub) <- colnames(df)
df[i,] <- sub
The threeas.character
statements could be combined withsapply(df,as.character)
– Silence Dogood
Apr 25 '14 at 7:06
1
thx... I need to start to use those pply functions more.
– Pigeon
Apr 25 '14 at 7:23
add a comment |
If you won't get shorter answer, this should help:
df=data.frame(x=c("l","m",NA,NA,"p"),y=c(NA,"b","c",NA,NA),z=c("u",NA,"w","x","y"))
sapply(df,as.character)
for(i in 1:nrow(df))
sub <- df[i,c(which(!is.na(df[i,])),which(is.na(df[i,])))]
colnames(sub) <- colnames(df)
df[i,] <- sub
The threeas.character
statements could be combined withsapply(df,as.character)
– Silence Dogood
Apr 25 '14 at 7:06
1
thx... I need to start to use those pply functions more.
– Pigeon
Apr 25 '14 at 7:23
add a comment |
If you won't get shorter answer, this should help:
df=data.frame(x=c("l","m",NA,NA,"p"),y=c(NA,"b","c",NA,NA),z=c("u",NA,"w","x","y"))
sapply(df,as.character)
for(i in 1:nrow(df))
sub <- df[i,c(which(!is.na(df[i,])),which(is.na(df[i,])))]
colnames(sub) <- colnames(df)
df[i,] <- sub
If you won't get shorter answer, this should help:
df=data.frame(x=c("l","m",NA,NA,"p"),y=c(NA,"b","c",NA,NA),z=c("u",NA,"w","x","y"))
sapply(df,as.character)
for(i in 1:nrow(df))
sub <- df[i,c(which(!is.na(df[i,])),which(is.na(df[i,])))]
colnames(sub) <- colnames(df)
df[i,] <- sub
edited Apr 25 '14 at 7:24
answered Apr 25 '14 at 6:30
PigeonPigeon
41328
41328
The threeas.character
statements could be combined withsapply(df,as.character)
– Silence Dogood
Apr 25 '14 at 7:06
1
thx... I need to start to use those pply functions more.
– Pigeon
Apr 25 '14 at 7:23
add a comment |
The threeas.character
statements could be combined withsapply(df,as.character)
– Silence Dogood
Apr 25 '14 at 7:06
1
thx... I need to start to use those pply functions more.
– Pigeon
Apr 25 '14 at 7:23
The three
as.character
statements could be combined with sapply(df,as.character)
– Silence Dogood
Apr 25 '14 at 7:06
The three
as.character
statements could be combined with sapply(df,as.character)
– Silence Dogood
Apr 25 '14 at 7:06
1
1
thx... I need to start to use those pply functions more.
– Pigeon
Apr 25 '14 at 7:23
thx... I need to start to use those pply functions more.
– Pigeon
Apr 25 '14 at 7:23
add a comment |
If you don't want to use VBA, you can try the following steps.
1. Select your dataset
2. Replace NA will empty cells
3. press F5 and select blanks ok
4. right click on any of the selection and delete (left)
I hope this helps.
add a comment |
If you don't want to use VBA, you can try the following steps.
1. Select your dataset
2. Replace NA will empty cells
3. press F5 and select blanks ok
4. right click on any of the selection and delete (left)
I hope this helps.
add a comment |
If you don't want to use VBA, you can try the following steps.
1. Select your dataset
2. Replace NA will empty cells
3. press F5 and select blanks ok
4. right click on any of the selection and delete (left)
I hope this helps.
If you don't want to use VBA, you can try the following steps.
1. Select your dataset
2. Replace NA will empty cells
3. press F5 and select blanks ok
4. right click on any of the selection and delete (left)
I hope this helps.
edited Oct 30 '18 at 18:48
SeGa
4,66431036
4,66431036
answered Jun 21 '15 at 5:45
szakwaniszakwani
318313
318313
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%2f23285215%2fshifting-non-na-cells-to-the-left%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