我有一个数据框 private static final NavigableMap<Long, String> suffixes = new TreeMap<>();
static {
suffixes.put(1_000L, "k");
suffixes.put(1_000_000L, "M");
suffixes.put(1_000_000_000L, "G");
suffixes.put(1_000_000_000_000L, "T");
suffixes.put(1_000_000_000_000_000L, "P");
suffixes.put(1_000_000_000_000_000_000L, "E");
}
public String format(long value) {
//Long.MIN_VALUE == -Long.MIN_VALUE so we need an adjustment here
if (value == Long.MIN_VALUE) return format(Long.MIN_VALUE + 1);
if (value < 0) return "-" + format(-value);
if (value < 1000) return Long.toString(value); //deal with easy case
Map.Entry<Long, String> e = suffixes.floorEntry(value);
Long divideBy = e.getKey();
String suffix = e.getValue();
long truncated = value / (divideBy / 10); //the number part of the output times 10
boolean hasDecimal = truncated < 100 && (truncated / 10d) != (truncated / 10);
return hasDecimal ? (truncated / 10d) + suffix : (truncated / 10) + suffix;
}
,其中包含0和非0值。例如:
a
我尝试使用0,0,0,30,0,05,0,0,0,0,0,6,0,0,7,0
获取非零索引,并且能够获得which(a!=0)
。但我的结果应该是这样的:
4 6 12 15
所以我需要的是作为第二列的连续出现之间的差异。对0的值可以是空白或0。
答案 0 :(得分:3)
x <- c(0,0,0,30,0,05,0,0,0,0,0,6,0,0,7,0)
创建匹配的矢量:
y <- rep(NA,length(x))
我用NA
填充空位;它真的不可能使用&#34;空白&#34;占位符在数字列中。您可以使用rep(0,length(x))
(或numeric(length(x))
代替。
找到非零元素:
nzpos <- which(x!=0)
使用适当的值填充非零位置:
y[nzpos] <- c(nzpos[1],diff(nzpos))
data.frame(x,y)
答案 1 :(得分:3)
要获得预期结果,您只需减去所获得的值:
which(a!=0) - c(0, which(a!=0)[-4]) # 4 2 6 3
然后,您可以使用此值创建新列。
a$B <- 0
a$B[which(a!=0)] <- which(a!=0) - c(0, which(a!=0)[-4])
输出:
A B
1 0 0
2 0 0
3 0 0
4 30 4
5 0 0
6 5 2
7 0 0
8 0 0
9 0 0
10 0 0
11 0 0
12 6 6
13 0 0
14 0 0
15 7 3
16 0 0
答案 2 :(得分:2)
您可以使用rle
功能计算连续的相等值:
DF <- data.frame(Col1=c(0,0,0,30,0,05,0,0,0,0,0,6,0,0,7,0),Col2=NA)
# count the length of groups of consecutive zeros
rleRes <- rle(DF$Col1==0)
# we add +1 since we include the next non-zero in the count
counts <- rleRes$lengths[rleRes$values] + 1
# if the last group of zero is not terminated by a non-zero,
# we don't need to add the last count
if(tail(DF$Col1,1) == 0)
counts <- counts[-length(counts)]
# if the first value is non-zero, then we need to set Count=0 (or NA) because
# is not preceded by a group of zero
if(head(DF$Col1,1) != 0)
counts <- c(0,counts)
# set the count on the second Column
DF$Col2[DF$Col1!=0] <- counts
> DF
Col1 Col2
1 0 NA
2 0 NA
3 0 NA
4 30 4
5 0 NA
6 5 2
7 0 NA
8 0 NA
9 0 NA
10 0 NA
11 0 NA
12 6 6
13 0 NA
14 0 NA
15 7 3
16 0 NA