我想離開的另一種方式。據我所知,Jay的例子有String_of_Nums
。鑑於你說strsplit()
工作,我假設你有String_of_Nums
作爲字符。在這裏,我也有專欄作爲人物。首先,拆分每個字符串(strsplit
),找到唯一字符(unique
),對字符進行排序(sort
),然後粘貼它們(toString
)。在這一點上,你有一個列表。您想要使用purrr
包中的as_vector
來轉換列表中的向量。有趣的是,我用基準,看看錶現會是怎麼樣來創建矢量(即Converted
)
library(magrittr)
library(purrr)
lapply(strsplit(mydf$String_of_Nums, split = ","),
function(x) toString(sort(unique(x)))) %>%
as_vector(.type = "character") -> mydf$out
# Month String_of_Nums out
#1 May 3,3,2 2, 3
#2 June 3,3,3,1 1, 3
#3 Sept 3,3,3,3 3
#4 Oct 3,3,3,4 3, 4
#5 Jan 3,3,4 3, 4
#6 Nov 3,3,5,5 3, 5
library(microbenchmark)
microbenchmark(
jazz = lapply(strsplit(mydf$String_of_Nums, split = ","),
function(x) toString(sort(unique(x)))) %>%
as_vector(.type = "character"),
jay = apply(read.csv(text=as.character(df1$String_of_Nums), header = FALSE), 1,
function(x) paste(sort(unique(x)), collapse = ",")),
times = 10000)
# expr min lq mean median uq max neval
# jazz 358.913 393.018 431.7382 405.9395 420.1735 54779.29 10000
# jay 1099.587 1151.244 1233.5631 1167.0920 1191.5610 56871.45 10000
DATA
Month String_of_Nums
1 May 3,3,2
2 June 3,3,3,1
3 Sept 3,3,3,3
4 Oct 3,3,3,4
5 Jan 3,3,4
6 Nov 3,3,5,5
mydf <- structure(list(Month = c("May", "June", "Sept", "Oct", "Jan",
"Nov"), String_of_Nums = c("3,3,2", "3,3,3,1", "3,3,3,3", "3,3,3,4",
"3,3,4", "3,3,5,5")), .Names = c("Month", "String_of_Nums"), row.names = c(NA,
-6L), class = "data.frame")
這個工作perfectly..Thanks周杰倫! – user2900006
當我嘗試在此處使用我的較大數據集時遇到問題。我能夠用月數據框運行這個很好,但是當我嘗試用較大的數據集執行此操作時,read.csv函數會將數字摺疊爲字符串(它不會在月份數據集中執行此操作),並創建比原始數據幀。我嘗試了'as.list'而不是'as.character',但是我得到一個錯誤。有任何想法嗎? – user2900006