如何在数据框中按行操作数据
How to manipulate data by row in a data frame
我有点糊涂了。我在数据框中有这样的数据
index times
1 1 56.60
2 1 150.75
3 1 204.41
4 2 44.71
5 2 98.03
6 2 112.20
而且我知道索引为 1 的时间有偏差,而索引为 1 的时间则没有。我需要创建该数据框的副本,以消除索引为 1 的样本中的偏差。我一直在尝试 apply、by 等的几种组合。我得到的最接近的是
by(lct, lct$index, function(x) { if(x$index == 1) x$times = x$times-50 else x$times = x$times } )
它返回了一个 class by
的对象,这对我来说是无法使用的。我需要以与原始文件相同的格式(索引、时间)将数据写回 csv 文件。想法?
像这样的东西应该可以工作:
df$times[df$index ==1] <- df$times[df$times == 1] - 50
这里的技巧是取 df$times
的子集以适合您的过滤器,并意识到 R 也可以分配给一个子集。
或者,您可以使用 ifelse
:
df$times = ifelse(df$index == 1, df$times - 50, df$times)
并在dplyr
中使用它:
library(dplyr)
df = data.frame(index = sample(1:5, 100, replace = TRUE),
value = runif(100)) %>% arrange(index)
df %>% mutate(value = ifelse(index == 1, value - 50, value))
# index value
#1 1 -49.95827
#2 1 -49.98104
#3 1 -49.44015
#4 1 -49.37316
#5 1 -49.76286
#6 1 -49.22133
#etc
怎么样,
index <- c(1, 1, 1, 2, 2, 2)
times <- c(56.60, 150.75, 204.41, 44.71, 98.03, 112.20)
df <- data.frame(index, times)
df$times <- ifelse(df$index == 1, df$times - 50, df$times)
> df
#index times
#1 1 6.60
#2 1 100.75
#3 1 154.41
#4 2 44.71
#5 2 98.03
#6 2 112.20
我有点糊涂了。我在数据框中有这样的数据
index times
1 1 56.60
2 1 150.75
3 1 204.41
4 2 44.71
5 2 98.03
6 2 112.20
而且我知道索引为 1 的时间有偏差,而索引为 1 的时间则没有。我需要创建该数据框的副本,以消除索引为 1 的样本中的偏差。我一直在尝试 apply、by 等的几种组合。我得到的最接近的是
by(lct, lct$index, function(x) { if(x$index == 1) x$times = x$times-50 else x$times = x$times } )
它返回了一个 class by
的对象,这对我来说是无法使用的。我需要以与原始文件相同的格式(索引、时间)将数据写回 csv 文件。想法?
像这样的东西应该可以工作:
df$times[df$index ==1] <- df$times[df$times == 1] - 50
这里的技巧是取 df$times
的子集以适合您的过滤器,并意识到 R 也可以分配给一个子集。
或者,您可以使用 ifelse
:
df$times = ifelse(df$index == 1, df$times - 50, df$times)
并在dplyr
中使用它:
library(dplyr)
df = data.frame(index = sample(1:5, 100, replace = TRUE),
value = runif(100)) %>% arrange(index)
df %>% mutate(value = ifelse(index == 1, value - 50, value))
# index value
#1 1 -49.95827
#2 1 -49.98104
#3 1 -49.44015
#4 1 -49.37316
#5 1 -49.76286
#6 1 -49.22133
#etc
怎么样,
index <- c(1, 1, 1, 2, 2, 2)
times <- c(56.60, 150.75, 204.41, 44.71, 98.03, 112.20)
df <- data.frame(index, times)
df$times <- ifelse(df$index == 1, df$times - 50, df$times)
> df
#index times
#1 1 6.60
#2 1 100.75
#3 1 154.41
#4 2 44.71
#5 2 98.03
#6 2 112.20