Using R to shift values to the left of data.frame [duplicate]

六眼飞鱼酱① 提交于 2019-12-18 09:32:47

问题


Okay, so I have this data.frame:

        A      B      C
1  yellow purple   <NA>
2    <NA>   <NA> yellow
3  orange yellow   <NA>
4  orange   <NA>  brown
5    <NA>  brown purple
6  yellow purple   pink
7  purple  green   pink
8  yellow   pink  green
9  purple orange   <NA>
10 purple   <NA>  brown

And I am interested in taking all the missing values from the first columns and replace them with the values over from the other columns, as an example with rows 2, 4, 5 and 10.

        A      B      C
1  yellow purple   <NA>
2  yellow   <NA>   <NA>
3  orange yellow   <NA>
4  orange  brown   <NA>
5   brown purple   <NA>
6  yellow purple   pink
7  purple  green   pink
8  yellow   pink  green
9  purple orange   <NA>
10 purple  brown   <NA>

My idea was to loop over the columns to grab the rows with the missing values and replace them with the values in the column to the right but that is also potentially flawed because what if there were 4 columns and two values in columns 2 and 3 were NA. Does anyone have an idea of an algorithm that may work?


回答1:


We can loop over the rows and concatenate the non-NA elements followed by the NA elements and assign it back to the dataset

df[] <-  t(apply(df, 1, function(x) c(x[!is.na(x)], x[is.na(x)])))
df
#        A      B     C
#1  yellow purple  <NA>
#2  yellow   <NA>  <NA>
#3  orange yellow  <NA>
#4  orange  brown  <NA>
#5   brown purple  <NA>
#6  yellow purple  pink
#7  purple  green  pink
#8  yellow   pink green
#9  purple orange  <NA>
#10 purple  brown  <NA>

data

df <- structure(list(A = c("yellow", NA, "orange", "orange", NA, "yellow", 
"purple", "yellow", "purple", "purple"), B = c("purple", NA, 
"yellow", NA, "brown", "purple", "green", "pink", "orange", NA
 ), C = c(NA, "yellow", NA, "brown", "purple", "pink", "pink", 
 "green", NA, "brown")), .Names = c("A", "B", "C"), row.names = c("1", 
 "2", "3", "4", "5", "6", "7", "8", "9", "10"), class = "data.frame")


来源:https://stackoverflow.com/questions/49079789/using-r-to-shift-values-to-the-left-of-data-frame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!