用不同的值替换数据框的前 n 个元素,同时保持其余部分不变

Replacing the first n elements of the data frame with a different value, while leaving the rest intact

我被困在某些事情中,我不知道如何摆脱它。

我需要完成的主要任务是将我的数据的 n%(这里是总数据的 10%)条目随机增加 20% 的值。并在单独的列中找到增加的值,而所有其他未增加的值将为 0。我想不出一种方法来根据索引捕获增加的值。所有数据最初都在一个数据框中。下面是我想要的数据的例子。

帮助将数据框的前 n 个元素替换为不同的值,同时保持其余部分不变。

所以,为了做到这一点,我想到了打乱数据并增加前 n% 的数据。虽然我能够增加数据,但我无法将其放回数据框中。完成计算后,该值以列表形式返回,当我尝试将其放回原处时,出现以下错误。

"Error in `[<-.data.frame`(`*tmp*`, "household_11", value = list(household_11 = c(360.481, :

replacement element 1 has 14 rows, need 144"



values <- read.csv("C:\hh_11.csv", header=TRUE)

oneday<- values[1441:1584,] #extracting data for Jan 11

set.seed(42)

shuffled= oneday[sample(1:nrow(oneday)), ]

n = as.integer((10/100)*nrow(oneday)) #select percentage of data to be changed, by 10%

multi <- function(a){

a*1.2 #select the percentage value to be increased, by 20%

}

top <- shuffled %<>% slice(1:10) %>% select("household_11") %>% lapply(FUN = multi)

oneday["household_11"]=top["household_11"]

任何帮助都会非常有帮助。我完全卡住了。 谢谢。

也许这会有所帮助:

df <- data.frame(household = round(runif(2000, 100, 2000), 2))
df$household_inc <- df$household
idx <- sample(nrow(df), 0.3*nrow(df))
df$household_inc[idx] <- df$household_inc[idx]*1.2
df$increased_value <- df$household_inc - df$household