是一种更快的方法来填充此矢量?

问题描述 投票:0回答:0
# Key let <- c("a", "b", "c") num <- c("one", "two", "three") # Given the following: v1 <- c("one", "two", "three", "two", "one") # Create the following using the key above: v2 <- c("a", "b", "c", "b", "a")

我已经使用了

data.table

,并取得了合理的成功,但是我想知道我是否忽略了策略。我希望能够以10亿+长度向量进行此操作,但正在陷入内存问题。
# EXAMPLE

# Create a number and letters that correspond to each other:
data_key <- c(1:100)
letter_class <- sample(letters, 100, replace = TRUE)

# Create vector of numbers
v1 <- sample(data_key, 1e8, replace = TRUE)
v2 <- c() # Make a v2 with letter_class that corresponds to number value in v1


# Create data with data_key and letter_class
key_table <- data.table(
  data_key,
  letter_class
)

d1 <- data.table(data_key = v1)

# Subset-method
t1 <- Sys.time()
v2_sub <- key_table[d1, , on = "data_key"][["letter_class"]]
Sys.time() - t1 
# Time difference of 3.457874 secs

# Merge-Method
t2 <- Sys.time()
v2_merge <- merge(d1,
            key_table,
            by = "data_key", 
            all.x = TRUE)[["letter_class"]]
Sys.time() - t2
# Time difference of 7.833402 secs
我有32GB的RAM。

使用Match()

溶解
# Match-Method
t3 <- Sys.time()
v2_match <- letter_class[match(v1, data_key)]
Sys.time() - t3
# Time difference of 0.821255 secs

r data.table
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.