我有一个层次结构,我想创建一个表来捕获最后一个值,之前的值和列号。
我不知道从哪里开始。我可以将列号计算为新列,然后我认为需要融合数据,但是我无法确定要使用什么变量,也不知道要搜索什么。
我的输入看起来像这样:
input = structure(list(V1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L), .Label = "ASIA PACIFIC", class = "factor"), V2 = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "AUSTRALIA", class = "factor"),
V3 = structure(c(1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("ACT",
"NEW SOUTH WALES"), class = "factor"), V4 = structure(c(1L,
3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 4L), .Label = c("CANBERRA",
"NEWCASTLE", "SYDNEY", "WOLLONGONG"), class = "factor"),
V5 = structure(c(9L, 2L, 6L, 4L, 7L, 3L, 5L, 8L, 10L, 1L), .Label = c("###",
"BONDI", "CAMPBELLTOWN", "GEORGE ST", "MAIN ST", "NEWTOWN",
"PITT ST", "POKOLBIN", "SMITH ST", "STRANGE PDE"), class = "factor"),
V6 = structure(c(1L, 2L, 3L, 1L, 1L, 5L, 1L, 4L, 1L, 1L), .Label = c("###",
"CHARLES AVE", "FRANCIS ST", "TOM ST", "TONY LANE"), class = "factor"),
V7 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "###", class = "factor")), class = "data.frame", row.names = c(NA,
-10L))
我正在寻找创建以下输出:
output =
structure(list(V1 = structure(c(10L, 3L, 4L, 5L, 9L, 14L, 6L,
13L, 11L, 15L, 2L, 12L, 8L, 7L, 1L), .Label = c("AUSTRALIA",
"CANBERRA", "CHARLES AVE", "FRANCIS ST", "GEORGE ST", "MAIN ST",
"NEW SOUTH WALES", "NEWCASTLE", "PITT ST", "SMITH ST", "STRANGE PDE",
"SYDNEY", "TOM ST", "TONY LANE", "WOLLONGONG"), class = "factor"),
V2 = structure(c(6L, 4L, 9L, 11L, 11L, 5L, 8L, 10L, 8L, 7L,
1L, 7L, 7L, 3L, 2L), .Label = c("ACT", "ASIA PACIFIC", "AUSTRALIA",
"BONDI", "CAMPBELLTOWN", "CANBERRA", "NEW SOUTH WALES", "NEWCASTLE",
"NEWTOWN", "POKOLBIN", "SYDNEY"), class = "factor"), V3 = c(5L,
6L, 6L, 5L, 5L, 6L, 5L, 6L, 5L, 4L, 3L, 3L, 3L, 2L, 1L)), class = "data.frame", row.names = c(NA,
-15L))
我不知道该如何寻找入门。如果有人可以建议我从哪里开始,我将不胜感激。
答案 0 :(得分:2)
通常,此类缺失值用NA
表示,但这里用###
表示,我们可以从每一行中删除这些值,然后使用tail
选择最后两个值并返回{{ 1}}。
length(new_x)
一种as.data.frame(t(apply(input, 1, function(x)
{new_x = x[x != "###"]; c(rev(tail(new_x, 2)), length(new_x)) })))
# V1 V2 V3
#1 CHARLES AVE BONDI 6
#2 FRANCIS ST NEWTOWN 6
#3 GEORGE ST SYDNEY 5
#4 PITT ST SYDNEY 5
#5 TONY LANE CAMPBELLTOWN 6
#6 MAIN ST NEWCASTLE 5
#7 TOM ST POKOLBIN 6
#8 STRANGE PDE NEWCASTLE 5
#9 WOLLONGONG NEW SOUTH WALES 4
的解决方法可能是
tidyverse