[英]Convert chararacter numbers to numeric in nested list with mixed types in R
[英]R repeat individual numbers within numeric sets in nested list
我有一組字母數字向量:
lst <- list(c("三垣3-19", "6", "81497", "79992", "79101",
"77760", "75973", "75411", "74666"), c("蒼龍1-01", "2", "66249", "65474", "66803", "64238"), c("蒼龍1-02", "1", "64238"), "蒼龍1-03")
[[1]]
[1] "三垣3-19" "6" "81497" "79992"
[5] "79101" "77760" "75973" "75411"
[9] "74666"
[[2]]
[1] "蒼龍1-01" "2" "66249" "65474"
[5] "66803" "64238"
[[3]]
[1] "蒼龍1-02" "1" "64238"
[[4]]
[1] "蒼龍1-03"
每個向量上的第二個數字(即6,2,1)代表要繪制的用於連接恆星的線的總數,由右側的HIP數給出。 每對HIP編號表示2星之間的線。
因此, [[1]]
81497 79992
意味着“在星號“ 81497”和“ 79992”之間划一條線,依此類推。
如果是連續的行,例如[[1]]
,則應重復“ 81497”和“ 74666”之間的數字,以使行中沒有中斷。
因此,在[[1]]
的情況下,應重復執行"79992" "79101" "77760" "75973" "75411"
以得到以下結果:
[[1]]
[1] "三垣3-19" "6" "81497" "79992"
[5] "79992" "79101" "79101" "77760"
[9] "77760" "75973" "75973" "75411"
[13] "75411" "74666"
[[2]]
[1] "蒼龍1-01" "2" "66249" "65474"
[5] "66803" "64238"
[[3]]
[1] "蒼龍1-02" "1" "64238" "64238"
[[4]]
[1] "蒼龍1-03"
由於每個列表中的第二個元素代表要繪制的線條總數,因此可以對有效性測試進行編碼,以指示是否需要重復某些數字。 因此6
在[[1]]
表示應該有6對遵循HIP號碼(即6 * 2 = 12個元素)。 當有效性測試失敗時,我希望R為我重復第三個元素與最后一個元素之間的數字,以便可以繪制連續線。
我設法解決的部分解決方案如下:
lapply(lst, function(x) x[2]) == (lengths(lst)-2)/2
[1] FALSE TRUE FALSE NA
這將測試HIP值的有效性。 原始列表中僅[[2]]
符合說明。 [[1]]
和[[3]]
將是我們需要處理的向量。
要在某個向量之間重復單個值,我可以這樣做:
> x <- c(1,2,3,4,5)
> x[2:4] <- lapply(x[2:4], function(x) rep(x, 2))
> unlist(x)
[1] 1 2 2 3 3 4 4 5
但是,因為lst
是一個列表,所以我不能這樣做:
lst[2:4] <- lapply(lst[2:4], function(x) rep(x, 2))
獲得相同的結果。 結束號(在這種情況下為4)需要由lengths(lst)
指定的事實使問題更加復雜。
我想最終的代碼將是一個ifelse()
函數,以連接上述兩個函數。
澄清規則:
每個向量的第二個元素代表畫一條線所需的不同HIP對數。
[[2]]
是有效的,因為后面緊跟着兩對數字,這些數字與第二個元素中給出的值匹配,因此不需要重復數字。
在這種情況下,這些線很可能形成十字線,而不是連續線。 因此,該規則應僅在連續線的情況下適用,例如[[1]]
。
對於[[3]]的情況,因為只有一個點,所以通常重復該數字,從而維持第二元素給出的有效性。
錯誤查詢
@TUSHAr:當向量中的元素包含非數字值時,您的代碼似乎會生成NA
值。
lst <- list(c("三垣3-19", "6", "81497", "79992A", "79101",
"77760", "75973A", "75411", "74666"), c("蒼龍1-01", "2", "66249", "65474", "66803B", "64238"), c("蒼龍1-02", "1", "64238"), "蒼龍1-03")
使用上面的數據運行代碼,您將獲得:
[[1]]
[1] "三垣3-19" "6" "81497" NA NA
[6] "79101" "79101" "77760" "77760" NA
[11] NA "75411" "75411" "74666"
[[2]]
[1] "蒼龍1-01" "2" "66249" "65474" NA
[6] "64238"
[[3]]
[1] "蒼龍1-02" "1" "64238" "64238"
[[4]]
[1] "蒼龍1-03"
是什么原因造成的,有辦法解決嗎?
存儲每個的所述第一值vector
在lst
在一個單獨的變量id
,以避免在處理過程中不必要的子集。
id = lapply(lst,function(t){t[1]})
刪除了已經存儲在id
的第一個元素。
lst = lapply(lst,function(t){
t=t[-1]
#if(length(t)>0){
# as.integer(t)
#}
})
通過經處理的循環lst
對象:
temp = lapply(lst,function(t){
#Use the first value as the desired number of pairs in `reqdpairs`
reqdpairs = as.numeric(t[1])
#remove the first values so that `t` only contains HIP numbers.
t=t[-1]
#calculate existing number of pairs for case [[2]] such that if all conditions are satisfied we don't do any processing
noofpairs = floor(length(t)/2)
#check if `t` contains values after removing the first element. The `else` part covers the case [[3]]
if(length(t)>1){
#If `noofpairs` is not equal to `reqdpairs` use `rep` on the inner elements (**excluding the first and last element**) of the vector.
if(noofpairs!=reqdpairs){
pairs=c(reqdpairs,t[1],rep(t[-c(1,length(t))],each=2),t[length(t)])
}else{
#In this case no processing is required so we just merge the reqdpairs with `t` as it is
pairs=c(reqdpairs,t)
}
}else if(length(t)==1){
pairs=rep(t[1],times=2)
pairs=c(reqdpairs,pairs)
}else{
pairs=NULL
}
pairs=as.character(pairs)
}
)
此步驟是將id
與temp
合並以獲得所需的輸出格式。 基本上只是一個串聯步驟。
mapply(function(x,y){c(x,y)},id,temp)
#[[1]]
#[1] "三垣3-19" "6" "81497" "79992" "79992" "79101" "79101" "77760" "77760" "75973"
#[11] "75973" "75411" "75411" "74666"
#[[2]]
#[1] "蒼龍1-01" "2" "66249" "65474" "66803" "64238"
#[[3]]
#[1] "蒼龍1-02" "1" "64238" "64238"
#[[4]]
#[1] "蒼龍1-03"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.