高效和选择性地组合R中的列
我有以下数据高效和选择性地组合R中的列,r,for-loop,apply,parallel-foreach,doparallel,R,For Loop,Apply,Parallel Foreach,Doparallel,我有以下数据 countrycols = alljson[,c("country_gc_str","country_ipapi_str","country_tm_str")] head(countrycols) country_gc_str country_ipapi_str country_tm_str 1 <NA> RU RU 2 <NA> C
countrycols = alljson[,c("country_gc_str","country_ipapi_str","country_tm_str")]
head(countrycols)
country_gc_str country_ipapi_str country_tm_str
1 <NA> RU RU
2 <NA> CN CN
3 US US US
4 <NA> CD CG
5 <NA> DE DE
6 <NA> <NA> NG
我还使用以下数据来描述国家收入水平:
wbURL <- "http://api.worldbank.org/countries?per_page=304"
xmlAPI <- xmlParse(wbURL)
xmlDF <- xmlToDataFrame(xmlAPI)
xmlDF$iso2CodeChar <- as.character(xmlDF$iso2Code)
xmlDF$incomeLevelChar <- as.character(xmlDF$incomeLevel)
incomexml <- xmlDF[,c("iso2CodeChar","incomeLevelChar")]
incomexmltable <- as.data.table(incomexml)
在三个变量中选取第一个非缺失值后,尝试使用矩阵索引:
countrycols[
cbind(
seq_len(nrow(countrycols)),
max.col(replace( -col(countrycols), is.na(countrycols), -Inf))
)
]
#[1] "RU" "CN" "US" "CD" "DE" "NG"
要解释逻辑,请分解每一行:
-col(countrycols)
# [,1] [,2] [,3]
#[1,] -1 -2 -3
#[2,] -1 -2 -3
#[3,] -1 -2 -3
#[4,] -1 -2 -3
#[5,] -1 -2 -3
#[6,] -1 -2 -3
replace( -col(countrycols), is.na(countrycols), -Inf)
# [,1] [,2] [,3]
#[1,] -Inf -2 -3
#[2,] -Inf -2 -3
#[3,] -1 -2 -3
#[4,] -Inf -2 -3
#[5,] -Inf -2 -3
#[6,] -Inf -Inf -3
(colindex <- max.col(replace( -col(countrycols), is.na(countrycols), -Inf)) )
#[1] 2 2 1 2 2 3
cbind(rowindex=seq_len(nrow(countrycols)), colindex)
# rowindex colindex
#[1,] 1 2
#[2,] 2 2
#[3,] 3 1
#[4,] 4 2
#[5,] 5 2
#[6,] 6 3
在三个变量中选取第一个非缺失值后,尝试使用矩阵索引:
countrycols[
cbind(
seq_len(nrow(countrycols)),
max.col(replace( -col(countrycols), is.na(countrycols), -Inf))
)
]
#[1] "RU" "CN" "US" "CD" "DE" "NG"
要解释逻辑,请分解每一行:
-col(countrycols)
# [,1] [,2] [,3]
#[1,] -1 -2 -3
#[2,] -1 -2 -3
#[3,] -1 -2 -3
#[4,] -1 -2 -3
#[5,] -1 -2 -3
#[6,] -1 -2 -3
replace( -col(countrycols), is.na(countrycols), -Inf)
# [,1] [,2] [,3]
#[1,] -Inf -2 -3
#[2,] -Inf -2 -3
#[3,] -1 -2 -3
#[4,] -Inf -2 -3
#[5,] -Inf -2 -3
#[6,] -Inf -Inf -3
(colindex <- max.col(replace( -col(countrycols), is.na(countrycols), -Inf)) )
#[1] 2 2 1 2 2 3
cbind(rowindex=seq_len(nrow(countrycols)), colindex)
# rowindex colindex
#[1,] 1 2
#[2,] 2 2
#[3,] 3 1
#[4,] 4 2
#[5,] 5 2
#[6,] 6 3
您看过
WDI
软件包了吗?很好。你看过WDI
软件包了吗?这是一个好的。
-col(countrycols)
# [,1] [,2] [,3]
#[1,] -1 -2 -3
#[2,] -1 -2 -3
#[3,] -1 -2 -3
#[4,] -1 -2 -3
#[5,] -1 -2 -3
#[6,] -1 -2 -3
replace( -col(countrycols), is.na(countrycols), -Inf)
# [,1] [,2] [,3]
#[1,] -Inf -2 -3
#[2,] -Inf -2 -3
#[3,] -1 -2 -3
#[4,] -Inf -2 -3
#[5,] -Inf -2 -3
#[6,] -Inf -Inf -3
(colindex <- max.col(replace( -col(countrycols), is.na(countrycols), -Inf)) )
#[1] 2 2 1 2 2 3
cbind(rowindex=seq_len(nrow(countrycols)), colindex)
# rowindex colindex
#[1,] 1 2
#[2,] 2 2
#[3,] 3 1
#[4,] 4 2
#[5,] 5 2
#[6,] 6 3
structure(list(country_gc_str = c(NA, NA, "US", NA, NA, NA),
country_ipapi_str = c("RU", "CN", "US", "CD", "DE", NA),
country_tm_str = c("RU", "CN", "US", "CG", "DE", "NG")), .Names = c("country_gc_str",
"country_ipapi_str", "country_tm_str"), row.names = c("1", "2",
"3", "4", "5", "6"), class = "data.frame")