以每行特定变量处理数据帧的行

问题描述 投票:1回答:3

我正在尝试通过唯一的采样区域将我在不同栏中的鱼类数量标准化。

我有以下简化的数据集:

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

我已经在这个问题上停留了超过一天。我衷心希望有人可以帮助我。提前非常感谢!

r database dataframe multiplying
3个回答
2
投票

我们可以使用sweep通过相应的Sample_surface应用行中的所有“鱼”列。

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)

0
投票

另一种选择是使用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)

0
投票

我们可以通过向量化的方式进行此操作

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)
© www.soinside.com 2019 - 2024. All rights reserved.