使用每行的特定变量操作数据框的行

Manipulate rows of a data frame with specific variables per row

我正在尝试通过一个独特的采样区域来标准化不同列中的鱼数量。

我得到了以下简化的数据集:

Year <- c(1990:2019) 
Location_nr <- c(1:30)
df <- data.frame(Year,Location_nr)
df$Sample_surface <- sample(10, size = nrow(df), replace = TRUE)
df$Fish1 <- sample(0:500, size = nrow(df), replace = TRUE)
df$Fish2 <- sample(0:500, size = nrow(df), replace = TRUE)
df$Fish3 <- sample(0:500, size = nrow(df), replace = TRUE)
df$Fish4 <- sample(0:500, size = nrow(df), replace = TRUE)

我想通过以下方式标准化每行的鱼数(列:Fish1、Fish2、Fish3 和 Fish4): (/"Sample_surface")/100

我已经在这个问题上卡了一天多了。我真诚地希望有人能帮助我解决这个问题。非常感谢!

我们可以使用sweep通过相应的Sample_surface应用行中的所有"Fish"列。

cols<- grep('Fish', names(df))
sweep(df[cols], 1, df$Sample_surface, `/`)/100

head(df)
#  Year Location_nr Sample_surface  Fish1 Fish2 Fish3 Fish4
#1 1990           1              3 1.0967 0.050 1.410 1.530
#2 1991           2              3 0.0733 0.383 1.223 0.703
#3 1992           3             10 0.4100 0.093 0.285 0.173
#4 1993           4              2 2.2150 1.305 1.975 1.360
#5 1994           5              6 0.5133 0.390 0.263 0.742
#6 1995           6              5 0.2680 0.910 0.240 0.602

或者我们可以使用 apply 按行

df[-c(1:3)] <- t(apply(df[-c(1:2)], 1, function(x) x[-1]/x[1]/100))

数据

set.seed(123)
Year <- c(1990:2019) 
Location_nr <- c(1:30)
df <- data.frame(Year,Location_nr)
df$Sample_surface <- sample(10, size = nrow(df), replace = TRUE)
df$Fish1 <- sample(0:500, size = nrow(df), replace = TRUE)
df$Fish2 <- sample(0:500, size = nrow(df), replace = TRUE)
df$Fish3 <- sample(0:500, size = nrow(df), replace = TRUE)
df$Fish4 <- sample(0:500, size = nrow(df), replace = TRUE)

另一种选择是使用 dplyrtidyr。在大多数情况下,建议以整洁的格式操作数据。

df %>%
  pivot_longer(-c(Year:Sample_surface), names_to = 'Fish', values_to = 'Value') %>%
  mutate(Value = Value / Sample_surface / 100) %>%
  pivot_wider(names_from = Fish, values_from = Value)

cbind(df %>% select(-starts_with('Fish')),
      df %>% select(starts_with('Fish')) / df$Sample_surface / 100)

我们可以用矢量化的方式做到这一点

cols<- grep('Fish', names(df))
df[cols] <- (df[cols]/df$Sample_surface)/100
head(df)
#  Year Location_nr Sample_surface      Fish1     Fish2     Fish3     Fish4
#1 1990           1              3 1.09666667 0.0500000 1.4100000 1.5300000
#2 1991           2              3 0.07333333 0.3833333 1.2233333 0.7033333
#3 1992           3             10 0.41000000 0.0930000 0.2850000 0.1730000
#4 1993           4              2 2.21500000 1.3050000 1.9750000 1.3600000
#5 1994           5              6 0.51333333 0.3900000 0.2633333 0.7416667
#6 1995           6              5 0.26800000 0.9100000 0.2400000 0.6020000

数据

set.seed(123)
Year <- c(1990:2019) 
Location_nr <- c(1:30)
df <- data.frame(Year,Location_nr)
df$Sample_surface <- sample(10, size = nrow(df), replace = TRUE)
df$Fish1 <- sample(0:500, size = nrow(df), replace = TRUE)
df$Fish2 <- sample(0:500, size = nrow(df), replace = TRUE)
df$Fish3 <- sample(0:500, size = nrow(df), replace = TRUE)
df$Fish4 <- sample(0:500, size = nrow(df), replace = TRUE)