2017-01-27 96 views
0

我目前正在通過「R數據科學」一書。在R中輸出作爲數據幀的輸出循環

我正在嘗試解決這個練習題(21.2.1 Q1.4),但在開始for循環之前無法確定正確的輸出。

寫一個for循環: 生成每個的μ= -10,0,10和100

就像在書前面的問題,我一直在試圖插入10層隨機法線到一個向量輸出,但對於這個例子,它似乎我需要輸出是一個數據幀?

這是我到目前爲止的代碼:

values <- c(-10,0,10,100) 
output <- vector("double", 10) 

for (i in seq_along(values)) { 
    output[[i]] <- rnorm(10, mean = values[[i]]) 
} 

我知道輸出是錯誤的,但我不能確定如何創建我需要這裏的格式。任何幫助非常感謝。謝謝!

+0

可以使用'lapply'或'sapply'即'D1 < - data.frame(lapply(值,函數(x)的RNORM(10 ,mean = x))); colnames(d1)< - paste0(「V」,seq_along(values))' – akrun

+0

嗨,感謝您的解決方案。如果能夠獲得for循環解決方案也是一件好事,因爲這是我目前旨在改進的一項技能。謝謝! – George

+0

您首先創建一個空矩陣或列表,然後爲其分配值。即'output < - vector(「list」,4)';並在必要時使用你的代碼和'cbind'或'rbind',即'do.call(cbind,output)' – akrun

回答

2

由於這是一個學習問題,我不會直接提供解決方案。

> values <- c(-10,0,10,100) 
> for (i in seq_along(values)) {print(i)} # Checking we iterate by position 
[1] 1 
[1] 2 
[1] 3 
[1] 4 
> output <- vector("double", 10) 
> output # Checking the place where the output will be 
[1] 0 0 0 0 0 0 0 0 0 0 
> for (i in seq_along(values)) { # Testing the full code 
+  output[[i]] <- rnorm(10, mean = values[[i]]) 
+ } 
Error in output[[i]] <- rnorm(10, mean = values[[i]]) : 
    more elements supplied than there are to replace 

正如你所看到的錯誤說,有更多的要素投入比空間(每個迭代產生10個隨機數字,(在總共40個),你只有10位。考慮使用的數據格式,允許存儲幾個值對於每次迭代 這樣:

> output <- ?? 
> for (i in seq_along(values)) { # Testing the full code 
+  output[[i]] <- rnorm(10, mean = values[[i]]) 
+ } 
> output # Should have length 4 and each element all the 10 values you created in the loop 
+0

使用上面的akrun的答案working:output < - vector(「list」,4)。這是你會做什麼?感謝所涉及的思考過程的徹底解釋!很有幫助! – George

+0

是或者簡單地使用'output < - list()'。你可以通過點擊勾號選擇一個最有用的答案嗎?這樣的問題被標記爲解決:D – Llopis

+0

非常感謝你! – George

2

有很多方法可以做到這一點。這是一個。請參閱行內評論。

set.seed(357) # to make things reproducible, set random seed 

N <- 10 # number of loops 

xy <- vector("list", N) # create an empty list into which values are to be filled 

# run the loop N times and on each loop... 
for (i in 1:N) { 
    # generate a data.frame with 4 columns, and add a random number into each one 
    # random number depends on the mean specified 
    xy[[i]] <- data.frame(um10 = rnorm(1, mean = -10), 
         u0 = rnorm(1, mean = 0), 
         u10 = rnorm(1, mean = 10), 
         u100 = rnorm(1, mean = 100)) 
} 

# result is a list of data.frames with 1 row and 4 columns 

# you can bind them together into one data.frame using do.call 
# rbind means they will be merged row-wise 
xy <- do.call(rbind, xy) 

     um10   u0  u10  u100 
1 -11.241117 -0.5832050 10.394747 101.50421 
2 -9.233200 0.3174604 9.900024 100.22703 
3 -10.469015 0.4765213 9.088352 99.65822 
4 -9.453259 -0.3272080 10.041090 99.72397 
5 -10.593497 0.1764618 10.505760 101.00852 
6 -10.935463 0.3845648 9.981747 100.05564 
7 -11.447720 0.8477938 9.726617 99.12918 
8 -11.373889 -0.3550321 9.806823 99.52711 
9 -7.950092 0.5711058 10.162878 101.38218 
10 -9.408727 0.5885065 9.471274 100.69328 

另一種方法是預先分配矩陣,添加值並將其強制爲data.frame。

xy <- matrix(NA, nrow = N, ncol = 4) 

for (i in 1:N) { 
    xy[i, ] <- rnorm(4, mean = c(-10, 0, 10, 100)) 
} 

# notice that i name the column names post festum 
colnames(xy) <- c("um10", "u0", "u10", "u100") 
xy <- as.data.frame(xy) 
0
# set the number of rows 
rows <- 10 

# vector with the values 
means <- c(-10,0,10,100) 

# generating output matrix 
output <- matrix(nrow = rows, 
      ncol = 4) 

# setting seed and looping through the number of rows 
set.seed(222) 
for (i in 1:rows){ 
    output[i,] <- rnorm(length(means), 
        mean=means) 
} 

#printing the output 
output