让R用回车写入.text

时间:2012-11-21 22:55:51

标签: r

我从数据中获取R样本100次然后将其写入文本文件,然后我的老板可以加载到Excel中。我目前有R做样本,但是当谈到写入时,我无法将结果放在单个列中,只是不同的行。我已经厌倦了writeLines,write.table,write和write.csv。我能得到的最接近的是使用write.table。

Dataset <- read.table("clipboard", header=FALSE, sep="", na.strings="", dec=".", strip.white=TRUE)
ThePath = ""
*Replace with where you want it to save
P = .25
*Put the Percentage Value you want to use here
X = round(nrow(Dataset)*P)
Boot1 = sum(sample(Dataset$V1, size=X))
Boot2 = sum(sample(Dataset$V1, size=X))
Boot3 = sum(sample(Dataset$V1, size=X))
Boot4 = sum(sample(Dataset$V1, size=X))
Boot5 = sum(sample(Dataset$V1, size=X))
Boot6 = sum(sample(Dataset$V1, size=X))
Boot7 = sum(sample(Dataset$V1, size=X))
Boot8 = sum(sample(Dataset$V1, size=X))
Boot9 = sum(sample(Dataset$V1, size=X))
Boot10 = sum(sample(Dataset$V1, size=X))
Boot11 = sum(sample(Dataset$V1, size=X))
Boot12 = sum(sample(Dataset$V1, size=X))
Boot13 = sum(sample(Dataset$V1, size=X))
Boot14 = sum(sample(Dataset$V1, size=X))
Boot15 = sum(sample(Dataset$V1, size=X))
Boot16 = sum(sample(Dataset$V1, size=X))
Boot17 = sum(sample(Dataset$V1, size=X))
Boot18 = sum(sample(Dataset$V1, size=X))
Boot19 = sum(sample(Dataset$V1, size=X))
Boot20 = sum(sample(Dataset$V1, size=X))
Boot21 = sum(sample(Dataset$V1, size=X))
Boot22 = sum(sample(Dataset$V1, size=X))
Boot23 = sum(sample(Dataset$V1, size=X))
Boot24 = sum(sample(Dataset$V1, size=X))
Boot25 = sum(sample(Dataset$V1, size=X))
Boot26 = sum(sample(Dataset$V1, size=X))
Boot27 = sum(sample(Dataset$V1, size=X))
Boot28 = sum(sample(Dataset$V1, size=X))
Boot29 = sum(sample(Dataset$V1, size=X))
Boot30 = sum(sample(Dataset$V1, size=X))
Boot31 = sum(sample(Dataset$V1, size=X))
Boot32 = sum(sample(Dataset$V1, size=X))
Boot33 = sum(sample(Dataset$V1, size=X))
Boot34 = sum(sample(Dataset$V1, size=X))
Boot35 = sum(sample(Dataset$V1, size=X))
Boot36 = sum(sample(Dataset$V1, size=X))
Boot37 = sum(sample(Dataset$V1, size=X))
Boot38 = sum(sample(Dataset$V1, size=X))
Boot39 = sum(sample(Dataset$V1, size=X))
Boot40 = sum(sample(Dataset$V1, size=X))
Boot41 = sum(sample(Dataset$V1, size=X))
Boot42 = sum(sample(Dataset$V1, size=X))
Boot43 = sum(sample(Dataset$V1, size=X))
Boot44 = sum(sample(Dataset$V1, size=X))
Boot45 = sum(sample(Dataset$V1, size=X))
Boot46 = sum(sample(Dataset$V1, size=X))
Boot47 = sum(sample(Dataset$V1, size=X))
Boot48 = sum(sample(Dataset$V1, size=X))
Boot49 = sum(sample(Dataset$V1, size=X))
Boot50 = sum(sample(Dataset$V1, size=X))
Boot51 = sum(sample(Dataset$V1, size=X))
Boot52 = sum(sample(Dataset$V1, size=X))
Boot53 = sum(sample(Dataset$V1, size=X))
Boot54 = sum(sample(Dataset$V1, size=X))
Boot55 = sum(sample(Dataset$V1, size=X))
Boot56 = sum(sample(Dataset$V1, size=X))
Boot57 = sum(sample(Dataset$V1, size=X))
Boot58 = sum(sample(Dataset$V1, size=X))
Boot59 = sum(sample(Dataset$V1, size=X))
Boot60 = sum(sample(Dataset$V1, size=X))
Boot61 = sum(sample(Dataset$V1, size=X))
Boot62 = sum(sample(Dataset$V1, size=X))
Boot63 = sum(sample(Dataset$V1, size=X))
Boot64 = sum(sample(Dataset$V1, size=X))
Boot65 = sum(sample(Dataset$V1, size=X))
Boot66 = sum(sample(Dataset$V1, size=X))
Boot67 = sum(sample(Dataset$V1, size=X))
Boot68 = sum(sample(Dataset$V1, size=X))
Boot69 = sum(sample(Dataset$V1, size=X))
Boot70 = sum(sample(Dataset$V1, size=X))
Boot71 = sum(sample(Dataset$V1, size=X))
Boot72 = sum(sample(Dataset$V1, size=X))
Boot73 = sum(sample(Dataset$V1, size=X))
Boot74 = sum(sample(Dataset$V1, size=X))
Boot75 = sum(sample(Dataset$V1, size=X))
Boot76 = sum(sample(Dataset$V1, size=X))
Boot77 = sum(sample(Dataset$V1, size=X))
Boot78 = sum(sample(Dataset$V1, size=X))
Boot79 = sum(sample(Dataset$V1, size=X))
Boot80 = sum(sample(Dataset$V1, size=X))
Boot81 = sum(sample(Dataset$V1, size=X))
Boot82 = sum(sample(Dataset$V1, size=X))
Boot83 = sum(sample(Dataset$V1, size=X))
Boot84 = sum(sample(Dataset$V1, size=X))
Boot85 = sum(sample(Dataset$V1, size=X))
Boot86 = sum(sample(Dataset$V1, size=X))
Boot87 = sum(sample(Dataset$V1, size=X))
Boot88 = sum(sample(Dataset$V1, size=X))
Boot89 = sum(sample(Dataset$V1, size=X))
Boot90 = sum(sample(Dataset$V1, size=X))
Boot91 = sum(sample(Dataset$V1, size=X))
Boot92 = sum(sample(Dataset$V1, size=X))
Boot93 = sum(sample(Dataset$V1, size=X))
Boot94 = sum(sample(Dataset$V1, size=X))
Boot95 = sum(sample(Dataset$V1, size=X))
Boot96 = sum(sample(Dataset$V1, size=X))
Boot97 = sum(sample(Dataset$V1, size=X))
Boot98 = sum(sample(Dataset$V1, size=X))
Boot99 = sum(sample(Dataset$V1, size=X))
Boot100 = sum(sample(Dataset$V1, size=X))
write.list(data.frame(sum(Boot1), sum(Boot2), sum(Boot3), sum(Boot4), sum(Boot5), sum(Boot6), sum(Boot7), sum(Boot8), sum(Boot9), sum(Boot10), sum(Boot11), sum(Boot12), sum(Boot13), sum(Boot14), sum(Boot15), sum(Boot16), sum(Boot17), sum(Boot18), sum(Boot19), sum(Boot20), sum(Boot21), sum(Boot22), sum(Boot23), sum(Boot24), sum(Boot25), sum(Boot26), sum(Boot27), sum(Boot28), sum(Boot29), sum(Boot30), sum(Boot31), sum(Boot32), sum(Boot33), sum(Boot34), sum(Boot35), sum(Boot36), sum(Boot37), sum(Boot38), sum(Boot39), sum(Boot40), sum(Boot41), sum(Boot42), sum(Boot43), sum(Boot44), sum(Boot45), sum(Boot46), sum(Boot47), sum(Boot48), sum(Boot49), sum(Boot50), sum(Boot51), sum(Boot52), sum(Boot53), sum(Boot54), sum(Boot55), sum(Boot56), sum(Boot57), sum(Boot58), sum(Boot59), sum(Boot60), sum(Boot61), sum(Boot62), sum(Boot63), sum(Boot64), sum(Boot65), sum(Boot66), sum(Boot67), sum(Boot68), sum(Boot69), sum(Boot70), sum(Boot71), sum(Boot72), sum(Boot73), sum(Boot74), sum(Boot75), sum(Boot76), sum(Boot77), sum(Boot78), sum(Boot79), sum(Boot80), sum(Boot81), sum(Boot82), sum(Boot83), sum(Boot84), sum(Boot85), sum(Boot86), sum(Boot87), sum(Boot88), sum(Boot89), sum(Boot90), sum(Boot91), sum(Boot92), sum(Boot93), sum(Boot94), sum(Boot95), sum(Boot96), sum(Boot97), sum(Boot98), sum(Boot99), sum(Boot100)), file=ThePath, row.name=FALSE, col.name=FALSE sep="/r")

我曾尝试使用write.list并且只是写但没有任何东西正在获取我正在寻找的输出。我也尝试将它变成一个csv,并将它作为一个sep进行处理,它们都像这样

25026689 / r19976650 / r13281740 / r15783000 / r36507540 / r15811400 / r15799460

或with,或/ r所在的空格。
我正在寻找像这样的东西

25026689
19976650个
13281740个
15783000个
36507540个
15811400个
15799460

我知道我的代码是超级强大的,并且可以通过计数和循环更清晰,更轻松地完成,但我仍然在学习大部分编码。

1 个答案:

答案 0 :(得分:3)

您的问题是您正在创建一行数据框。在Excel中,这将以相同的方式表示(即一行,而不是一列)。

解决方案是创建一个包含引导值的矢量。

这是熟悉sapply或其近亲replicate的最佳时机:

boot <- data.frame(
  boot = replicate(100, sum(runif(100)))
)
head(boot)
      boot
1 50.84482
2 49.57098
3 52.75195
4 52.20751
5 48.55071
6 50.76622

很简单,不是吗?


现在你的代码变成了:

Dataset <- read.table("clipboard", header=FALSE, sep="", na.strings="", 
                      dec=".", strip.white=TRUE)
ThePath <-  ""
#Replace with where you want it to save
P <- .25
#Put the Percentage Value you want to use here
X <- round(nrow(Dataset)*P)


boot <- data.frame(
  boot = replicate(100, sum(sample(Dataset$V1, size=X)))
)
write.csv(boot, file=="your_file_name", row.names=FALSE)

免责声明:未经测试 - 我没有您的数据