Reputation: 858
I have a data frame formatted like this in R:
How could I call up all the HEIGHT values where GROUP is equal to B and change them? I.e. from cm to mm.
Upvotes: 2
Views: 56633
Reputation: 1101
data = data.frame(
group = rep(c("A", "B", "C"), each = 4),
height = c(259, 243, 253, 235, 23.5, 23.6, 23.5, 24.1, 235, 234, 235, 220)
)
data #shows your data
#from cm to mm
data$height[data$group == "B"] <- data$height[data$group == "B"] * 10
Upvotes: 17
Reputation: 61154
Something like this?
> set.seed(1) # simulating some data
> df <- data.frame(group=rep(c("A", "B", "C"), each=4), height=sample(100:150, 12))
> df
group height
1 A 113
2 A 118
3 A 128
4 A 143
5 B 109
6 B 141
7 B 142
8 B 129
9 C 127
10 C 102
11 C 108
12 C 107
> # changing `height` for group `B`
> ind <- df$group %in% "B" # locating group B
> df[ind, 2] <- df[ind, 2] * 10 # changing from cm to mm
> df
group height
1 A 113
2 A 118
3 A 128
4 A 143
5 B 1090
6 B 1410
7 B 1420
8 B 1290
9 C 127
10 C 102
11 C 108
12 C 107
Upvotes: 4