我有这样一个数据框:
df <- structure(list(a = c(NA, NA, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L), b = c(NA, NA, NA, 1L, 2L, 3L, 4L, 5L, 6L, 7L), d = c(NA, NA, NA, NA, 1L, 2L, 3L, 4L, 5L, 6L)), .Names = c("a", "b", "d"), row.names = c(NA, -10L), class = "data.frame")
> df
a b d
1 NA NA NA
2 NA NA NA
3 1 NA NA
4 2 1 NA
5 3 2 1
6 4 3 2 …Run Code Online (Sandbox Code Playgroud) 我有一个这样的数据集
temp <- structure(list(col_1 = c("", "P9603", "", "", "11040",
"80053"), col_2 = c("84484", "80061", "", "80061", "A0428", "85025"
), col_3 = c("V2632", "82310", "", "", "", "86357"), col_4 = c("J1170",
"84305", "62311", "80061", "", ""), col_5 = c("", "86708", "J0690",
"", "", "")), .Names = c("col_1", "col_2", "col_3", "col_4",
"col_5"), class = c("data.table", "data.frame"))
col_1 col_2 col_3 col_4 col_5
1: 84484 V2632 J1170
2: P9603 80061 82310 84305 86708
3: 62311 J0690
4: 80061 80061
5: 11040 A0428
6: …Run Code Online (Sandbox Code Playgroud) 我的数据集中有许多NA,我需要将所有这些单元格(在行级别)向左移动.
示例 - 我的数据帧:
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
Run Code Online (Sandbox Code Playgroud)
我希望上面的数据帧转换成这个:
x y z
1 l u NA
2 m b NA
3 c w NA
4 x <NA> NA
5 p y NA
Run Code Online (Sandbox Code Playgroud)
请帮忙.
谢谢.