Skip to content

Commit 80ec759

Browse files
committed
updated frank condition
1 parent 5483d48 commit 80ec759

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

R/frank.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ frankv = function(x, cols=seq_along(x), order=1L, na.last=TRUE, ties.method=c("a
1616
if (!missing(cols) && !is.null(cols))
1717
stopf("x is a single vector, non-NULL 'cols' doesn't make sense")
1818
cols = 1L
19+
x = copy(x) # Deep copy atomic vectors
1920
x = as_list(x)
2021
} else {
2122
cols = colnamesInt(x, cols, check_dups=TRUE)
2223
if (!length(cols))
2324
stopf("x is a list, 'cols' can not be 0-length")
25+
if (!is.data.frame(x))
26+
x = copy(x)
2427
}
2528
# need to unlock for #4429
2629
x = .shallow(x, cols, unlock = TRUE) # shallow copy even if list..

inst/tests/tests.Rraw

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21167,3 +21167,38 @@ test(2317.6, DT1[DF1, on='a', .(d = x.a + i.d)]$d, 5)
2116721167
test(2317.7, DT1[DF2, on='a', e := i.e]$e, 5)
2116821168
test(2317.8, DT1[DF2, on='a', e2 := x.a + i.e]$e2, 6)
2116921169
test(2317.9, DT1[DF2, on='a', .(e = x.a + i.e)]$e, 6)
21170+
21171+
# frank() should not strip names from named vector input
21172+
x = c(a = "a", b = "b", c = "a")
21173+
test(2318.01, { frank(x); names(x) }, c("a", "b", "c"))
21174+
21175+
# frank() should not modify names inside a list of named vectors
21176+
ls = list(a = c(a = 1, b = 2), b = c(a = 2, b = 3))
21177+
frank(ls)
21178+
test(2318.02, names(ls[[1]]), c("a", "b"))
21179+
test(2318.03, names(ls[[2]]), c("a", "b"))
21180+
21181+
# frank() should not mutate original list (deep equality)
21182+
ls = list(a = c(a = 10, b = 20), b = c(a = 30, b = 40))
21183+
original_ls = list(a = c(a = 10, b = 20), b = c(a = 30, b = 40))
21184+
frank(ls)
21185+
test(2318.04, identical(ls, original_ls), TRUE)
21186+
21187+
# frank() should not mutate list of unnamed vectors (control)
21188+
ls = list(a = c(1, 2), b = c(2, 3))
21189+
original_ls = list(a = c(1, 2), b = c(2, 3))
21190+
frank(ls)
21191+
test(2318.05, identical(ls, original_ls), TRUE)
21192+
21193+
# frank() should return correct ranks on data.table column without side effect
21194+
DT = data.table(x = c(3, 2, 1, 3))
21195+
original_x = DT$x
21196+
ranks = frank(DT$x)
21197+
test(2318.06, ranks, c(3.5, 2, 1, 3.5))
21198+
test(2318.07, identical(DT$x, original_x), TRUE)
21199+
21200+
# frank() should preserve names on factors
21201+
x = factor(c("low", "medium", "high"), levels = c("low", "medium", "high"))
21202+
names(x) = c("a", "b", "c")
21203+
frank(x)
21204+
test(2318.08, names(x), c("a", "b", "c"))

0 commit comments

Comments
 (0)