计算非连续数字之间的差异(列表中的第一个实例)

时间:2014-10-23 17:14:47

标签: r string indexing

我在data.frame中有一个列,其中每个观察都是一串数字(例如" 1,5,6,7,0,21和#34;)。我试图计算第一个非连续数字的差异。在上面的示例中,结果为5 - 1 = 4。但是,根据我目前的代码,我得到了6。如果我的输入是" 1,2,0,21"我得到21 - 2 = 19的正确结果(数字在减法发生之前排序)。我想也许零是问题,但在所有值中添加一个并没有解决问题。也许我的索引存在问题?有什么建议?

# find distance between number in first gap of non-consecutive numbers
b <- c("1,5,6,7,0,21") # does not work as desired result is 6 instead of 4
# b <- ("1,2,0,21") # works as desired

b.Uncomma <- sort(unique(as.numeric(unlist(strsplit(b, split=","))))) # remove commas, remove duplicates, sort
#b.Uncomma <- b.Uncomma + 1 # same result
b.Gaps <- c(which(diff(b.Uncomma) != 1), length(b.Uncomma)) # find where the difference is not 1
b.FirstGap <- b.Gaps[1:2] # get the positions/index on either side of the first gap
b.Result <- b.Uncomma[(b.FirstGap[2])] - b.Uncomma[(b.FirstGap[1])] # subtract to get result

2 个答案:

答案 0 :(得分:2)

inp <- scan(text=b,sep=",")
#Read 6 items
 sinp <- sort(inp)
 diff(sinp)
#[1]  1  4  1  1 14

> diff(sinp)[diff(sinp) != 1][1]
#[1] 4

答案 1 :(得分:1)

尝试:

 b.Uncomma <- sort(unique(as.numeric(unlist(strsplit(b, split=","))))) # remove commas, remove duplicates, sort
 b.Gaps <- c(which(diff(b.Uncomma) != 1), length(b.Uncomma)) # find where the difference is not 1
 b.FirstGap <- b.Gaps[1] # get the positions/index of the first gap 
 b.Result <- b.Uncomma[(b.FirstGap+1)] - b.Uncomma[(b.FirstGap)] # subtract to get result
 b.Result