将行添加到数据框
Adding row to a dataframe
我有一个这样的 df:
x <- data.frame("Year" = c(1945,1945,1946,1946,1947,1947), "Age" = c(1,2,1,2,1,2), "Value" = c(4,5,4,5,4,6))
我想根据某些条件添加新行:
例如,我想添加与第一行具有相同“YEAR”和“AGE”的第二行,但值是第一行的值减去 1。
我想要这样的结果:
x <- data.frame("Year" = c(1945, 1945,1945,1945,1946,1946,1946,1946,1947,1947,1947,1947), "Age" = c(1,1,2,2,1,1,2,2,1,1,2,2), "Value" = c(4,3,5,4,4,3,5,4,4,3,5,4))
感谢您帮助初学者。
你可以这样做:
#original df
x <- data.frame("Year" = c(1945,1945,1946,1946,1947,1947), "Age" = c(1,2,1,2,1,2),"Value" = c(4,5,4,5,4,6))
#replicate df with value minus 1
y <- data.frame(x[,c("Year", "Age")], Value = x[,"Value"] -1)
#combine
z <- rbind(x,y)
z[order(z$Year, z$Age),]
Year Age Value
1 1945 1 4
7 1945 1 3
2 1945 2 5
8 1945 2 4
3 1946 1 4
9 1946 1 3
4 1946 2 5
10 1946 2 4
5 1947 1 4
11 1947 1 3
6 1947 2 6
12 1947 2 5
上面的代码当然可以缩短,例如直接更新数据框 x 时的代码。
x <- rbind(x, data.frame(x[,c("Year", "Age")], Value = x[,"Value"] -1))
正如下面的评论所指出的,可以使用 transform()
或 within()
.
进一步缩短代码(并使其更具可读性)
x <- rbind(x, transform(x, Value = Value - 1))
x <- rbind(x, within(x, Value <- Value - 1))
这个有用吗:
library(dplyr)
x %>% inner_join(x %>% select(1)) %>% group_by(Year, Age) %>%
mutate(Value = case_when(row_number() == 2 ~ Value - 1, TRUE ~ Value))
Joining, by = "Year"
# A tibble: 12 x 3
# Groups: Year, Age [6]
Year Age Value
<dbl> <dbl> <dbl>
1 1945 1 4
2 1945 1 3
3 1945 2 5
4 1945 2 4
5 1946 1 4
6 1946 1 3
7 1946 2 5
8 1946 2 4
9 1947 1 4
10 1947 1 3
11 1947 2 6
12 1947 2 5
使用的数据:
x
Year Age Value
1 1945 1 4
2 1945 2 5
3 1946 1 4
4 1946 2 5
5 1947 1 4
6 1947 2 6
在base
中,你可以将每一行重复两次,并将回收的0:-1
添加到Value
。
within(x[rep(1:nrow(x), each = 2), ], Value <- Value + 0:-1)
它的tidyverse
版本是
library(tidyverse)
x %>%
uncount(2) %>%
mutate(Value = Value + 0:-1)
他们都给出了以下内容。输出已经排序好,所以你不需要使用 order()
或 arrange()
.
Year Age Value
1 1945 1 4
2 1945 1 3
3 1945 2 5
4 1945 2 4
5 1946 1 4
6 1946 1 3
7 1946 2 5
8 1946 2 4
9 1947 1 4
10 1947 1 3
11 1947 2 6
12 1947 2 5
我有一个这样的 df:
x <- data.frame("Year" = c(1945,1945,1946,1946,1947,1947), "Age" = c(1,2,1,2,1,2), "Value" = c(4,5,4,5,4,6))
我想根据某些条件添加新行: 例如,我想添加与第一行具有相同“YEAR”和“AGE”的第二行,但值是第一行的值减去 1。
我想要这样的结果:
x <- data.frame("Year" = c(1945, 1945,1945,1945,1946,1946,1946,1946,1947,1947,1947,1947), "Age" = c(1,1,2,2,1,1,2,2,1,1,2,2), "Value" = c(4,3,5,4,4,3,5,4,4,3,5,4))
感谢您帮助初学者。
你可以这样做:
#original df
x <- data.frame("Year" = c(1945,1945,1946,1946,1947,1947), "Age" = c(1,2,1,2,1,2),"Value" = c(4,5,4,5,4,6))
#replicate df with value minus 1
y <- data.frame(x[,c("Year", "Age")], Value = x[,"Value"] -1)
#combine
z <- rbind(x,y)
z[order(z$Year, z$Age),]
Year Age Value
1 1945 1 4
7 1945 1 3
2 1945 2 5
8 1945 2 4
3 1946 1 4
9 1946 1 3
4 1946 2 5
10 1946 2 4
5 1947 1 4
11 1947 1 3
6 1947 2 6
12 1947 2 5
上面的代码当然可以缩短,例如直接更新数据框 x 时的代码。
x <- rbind(x, data.frame(x[,c("Year", "Age")], Value = x[,"Value"] -1))
正如下面的评论所指出的,可以使用 transform()
或 within()
.
x <- rbind(x, transform(x, Value = Value - 1))
x <- rbind(x, within(x, Value <- Value - 1))
这个有用吗:
library(dplyr)
x %>% inner_join(x %>% select(1)) %>% group_by(Year, Age) %>%
mutate(Value = case_when(row_number() == 2 ~ Value - 1, TRUE ~ Value))
Joining, by = "Year"
# A tibble: 12 x 3
# Groups: Year, Age [6]
Year Age Value
<dbl> <dbl> <dbl>
1 1945 1 4
2 1945 1 3
3 1945 2 5
4 1945 2 4
5 1946 1 4
6 1946 1 3
7 1946 2 5
8 1946 2 4
9 1947 1 4
10 1947 1 3
11 1947 2 6
12 1947 2 5
使用的数据:
x
Year Age Value
1 1945 1 4
2 1945 2 5
3 1946 1 4
4 1946 2 5
5 1947 1 4
6 1947 2 6
在base
中,你可以将每一行重复两次,并将回收的0:-1
添加到Value
。
within(x[rep(1:nrow(x), each = 2), ], Value <- Value + 0:-1)
它的tidyverse
版本是
library(tidyverse)
x %>%
uncount(2) %>%
mutate(Value = Value + 0:-1)
他们都给出了以下内容。输出已经排序好,所以你不需要使用 order()
或 arrange()
.
Year Age Value
1 1945 1 4
2 1945 1 3
3 1945 2 5
4 1945 2 4
5 1946 1 4
6 1946 1 3
7 1946 2 5
8 1946 2 4
9 1947 1 4
10 1947 1 3
11 1947 2 6
12 1947 2 5