我需要进行一千次试验,每次试验产生一个12行7列的矩阵。如何创建一个多路矩阵来保持这个结果。 感谢
答案 0 :(得分:2)
据推测,方便的方法是将矩阵合并到一个列表中:
A <- matrix(1, ncol=7, nrow=12)
B <- matrix(2, ncol=7, nrow=12)
C <- matrix(3, ncol=7, nrow=12)
mats_list <- list(A, B, C)
这将为您提供一个对象,每个对象具有单独的维度,每个矩阵,然后是每个矩阵中的行/列。我会说这足以满足您的需求,但如果您愿意,可以使用array
:
# List to matrix
mat_mat <- array(data = mats_list)
dim(mat_mat)
为了得到:
> dim(mat_mat)
[1] 3
作为
> dim(mats_list)
NULL
这就是说,列表绝对可以存储和提取结果。
答案 1 :(得分:1)
您可以使用数组:
set.seed(2)
x = array(rnorm(1000*12*7), c(1000,12,7))
> dim(x[1,,])
#[1] 12 7
> x[1,,]
# [,1] [,2] [,3] [,4] [,5] [,6] [,7]
# [1,] -0.89691455 1.4948667 0.4972043 0.19952489 -0.979683918 -0.71217954 0.21510070
# [2,] 0.46620043 -0.7645256 -0.7172315 1.69938645 -1.344280437 0.20653442 -0.91194045
# [3,] -1.89234891 -0.1624283 1.1351026 -1.03220736 0.515821229 -0.10112438 -0.85460968
# [4,] -2.13847638 -0.8206868 0.4083286 0.01454552 1.373745316 -0.02894237 -0.39673381
# [5,] 0.64084454 -1.0644555 -1.2606268 -1.69156964 -0.788296665 1.13059108 0.03310462
# [6,] -1.73081229 0.5706814 -1.2607599 0.16596195 0.001174951 0.55506678 -1.36083280
# [7,] -1.00846517 -0.8213516 1.7968959 1.23328148 -0.682850261 1.14101830 0.84107576
# [8,] 1.02356724 -0.9281833 0.3441365 -0.01183211 -0.649788421 0.08010038 -0.50363132
# [9,] -0.02165721 0.6806610 0.9703106 1.22171432 -1.446446015 1.34022520 -1.41818567
#[10,] 0.77058313 0.5023534 1.1598751 -1.20300804 -0.367739799 -0.95578416 -0.14794308
#[11,] 1.34046771 -1.5331327 0.9604876 -0.40545479 -1.066988497 -0.14925083 -1.01576090
#[12,] -0.60441747 0.1111074 1.8406487 0.19648890 0.503018762 1.01263304 0.42761716
答案 2 :(得分:1)
让我们假设您有一个名为{"created_at":"Thu Jul 02 18:59:13 +0000 2015","id":616682557896290306,"id_str":"616682557896290306","text":"I am #testing again","source":"\u003ca href=\"https:\/\/about.twitter.com\/products\/tweetdeck\" rel=\"nofollow\"\u003eTweetDeck\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":247344597,"id_str":"247344597","name":"Techniponi","screen_name":"techniponi","location":"Sugar Land, TX","url":"http:\/\/comeonandsl.am","description":"Internet Marketing Specialist for Wolf Beats, weekly DJ on PonyvilleFM (Sundays 4-5pm Central). I made music like a year ago. Skype wincam98","protected":false,"verified":false,"followers_count":110,"friends_count":187,"listed_count":3,"favourites_count":353,"statuses_count":806,"created_at":"Fri Feb 04 16:14:13 +0000 2011","utc_offset":-18000,"time_zone":"Central Time (US & Canada)","geo_enabled":false,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"000000","profile_background_image_url":"http:\/\/pbs.twimg.com\/profile_background_images\/612795294728597504\/XISJ1ccp.png","profile_background_image_url_https":"https:\/\/pbs.twimg.com\/profile_background_images\/612795294728597504\/XISJ1ccp.png","profile_background_tile":true,"profile_link_color":"3B94D9","profile_sidebar_border_color":"000000","profile_sidebar_fill_color":"000000","profile_text_color":"000000","profile_use_background_image":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/612347971368148992\/Qeoo3RvD_normal.png","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/612347971368148992\/Qeoo3RvD_normal.png","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/247344597\/1431372460","default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[{"text":"testing","indices":[5,13]}],"trends":[],"urls":[],"user_mentions":[],"symbols":[]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"en","timestamp_ms":"1435863553867"}
的函数,它生成一个12x7矩阵。
e.g。
trial
使用基数R,您可以这样做:
trial <- function() {
matrix(rnorm(84), nrow = 12)
}
将生成一个包含3维或
的数组trial_array <- replicate(1000, trial())
等效的trial_list <- replicate(1000, trial(), simplify = FALSE)
函数为plyr
和raply
。