Hello, friends here in posit community. It is really sorry to bother, but I have been stuck with an elementary(probably) problem for a couple days and eager for emergent help. The scene is like this. I set up an Excel table with variables and instances stored in it days ago. To make it clear, let's just say every row stands for a particular customer and every column their marks given at different dishes. Now I need to update part of it and retain the rest, like, to change the marks given by Customer_1, Customer_2, Customer_11, Customer_25 (and many more) at salad, pizza and chicken roll from the previous values to newly collected ones. Are there elegant ways for suggestion? Many thanks!
And more specially, what if I have also stored their address, gender, economic status and more attributes, and marks given at different times, and want to change the many marks based on the specific values of these attributes while retaining the rest of the whole table unchanged? Any recommendation on packages and functions will also be appreciated!
See the FAQ: How to do a minimal reproducible example reprex
for beginners to attract more specific answers.
d <- data.frame(
id = 1:6,
mpg = c(21, 21, 22.8, 21.4, 18.7, 18.1), cyl = c(6, 6, 4, 6, 8, 6),
disp = c(160, 160, 108, 258, 360, 225),
hp = c(110, 110, 93, 110, 175, 105),
drat = c(3.9, 3.9,3.85, 3.08, 3.15, 2.76),
wt = c(2.62, 2.875, 2.32, 3.215, 3.44,3.46),
qsec = c(16.46, 17.02, 18.61, 19.44, 17.02, 20.22),
vs = c( 0,0, 1, 1, 0, 1),
am = c(1, 1, 1, 0, 0, 0),
gear = c(4, 4, 4, 3,3, 3),
carb = c(4, 4, 1, 1, 2, 1))
d
#> id mpg cyl disp hp drat wt qsec vs am gear carb
#> 1 1 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
#> 2 2 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
#> 3 3 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
#> 4 4 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
#> 5 5 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
#> 6 6 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
# change mpg for id 1 to 22.0 (row1,col1)
d[1,1] <- 22
d
#> id mpg cyl disp hp drat wt qsec vs am gear carb
#> 1 22 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
#> 2 2 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
#> 3 3 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
#> 4 4 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
#> 5 5 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
#> 6 6 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
# change disp for id1 and id2 to 180
d[1:2,4] <- 180
d
#> id mpg cyl disp hp drat wt qsec vs am gear carb
#> 1 22 21.0 6 180 110 3.90 2.620 16.46 0 1 4 4
#> 2 2 21.0 6 180 110 3.90 2.875 17.02 0 1 4 4
#> 3 3 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
#> 4 4 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
#> 5 5 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
#> 6 6 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
# change all carb to 4
d[12] <- 4
d
#> id mpg cyl disp hp drat wt qsec vs am gear carb
#> 1 22 21.0 6 180 110 3.90 2.620 16.46 0 1 4 4
#> 2 2 21.0 6 180 110 3.90 2.875 17.02 0 1 4 4
#> 3 3 22.8 4 108 93 3.85 2.320 18.61 1 1 4 4
#> 4 4 21.4 6 258 110 3.08 3.215 19.44 1 0 3 4
#> 5 5 18.7 8 360 175 3.15 3.440 17.02 0 0 3 4
#> 6 6 18.1 6 225 105 2.76 3.460 20.22 1 0 3 4
# change all gear to 4
d$gear <- 4
d
#> id mpg cyl disp hp drat wt qsec vs am gear carb
#> 1 22 21.0 6 180 110 3.90 2.620 16.46 0 1 4 4
#> 2 2 21.0 6 180 110 3.90 2.875 17.02 0 1 4 4
#> 3 3 22.8 4 108 93 3.85 2.320 18.61 1 1 4 4
#> 4 4 21.4 6 258 110 3.08 3.215 19.44 1 0 4 4
#> 5 5 18.7 8 360 175 3.15 3.440 17.02 0 0 4 4
#> 6 6 18.1 6 225 105 2.76 3.460 20.22 1 0 4 4
# change id4 hp and drat
d[3,5:6] <- c(100,3.9)
d
#> id mpg cyl disp hp drat wt qsec vs am gear carb
#> 1 22 21.0 6 180 110 3.90 2.620 16.46 0 1 4 4
#> 2 2 21.0 6 180 110 3.90 2.875 17.02 0 1 4 4
#> 3 3 22.8 4 108 100 3.90 2.320 18.61 1 1 4 4
#> 4 4 21.4 6 258 110 3.08 3.215 19.44 1 0 4 4
#> 5 5 18.7 8 360 175 3.15 3.440 17.02 0 0 4 4
#> 6 6 18.1 6 225 105 2.76 3.460 20.22 1 0 4 4
# change any am 1 to am 0
d[which(d$am == 1),"am"] <- 0
d
#> id mpg cyl disp hp drat wt qsec vs am gear carb
#> 1 22 21.0 6 180 110 3.90 2.620 16.46 0 0 4 4
#> 2 2 21.0 6 180 110 3.90 2.875 17.02 0 0 4 4
#> 3 3 22.8 4 108 100 3.90 2.320 18.61 1 0 4 4
#> 4 4 21.4 6 258 110 3.08 3.215 19.44 1 0 4 4
#> 5 5 18.7 8 360 175 3.15 3.440 17.02 0 0 4 4
#> 6 6 18.1 6 225 105 2.76 3.460 20.22 1 0 4 4
# change qsec to 20 if vs == 1 and hp > 105
d[which(d$vs == 1 & d$hp > 105),8] <- 20
d
#> id mpg cyl disp hp drat wt qsec vs am gear carb
#> 1 22 21.0 6 180 110 3.90 2.620 16.46 0 0 4 4
#> 2 2 21.0 6 180 110 3.90 2.875 17.02 0 0 4 4
#> 3 3 22.8 4 108 100 3.90 2.320 18.61 1 0 4 4
#> 4 4 21.4 6 258 110 3.08 3.215 20.00 1 0 4 4
#> 5 5 18.7 8 360 175 3.15 3.440 17.02 0 0 4 4
#> 6 6 18.1 6 225 105 2.76 3.460 20.22 1 0 4 4
Created on 2023-06-12 with reprex v2.0.2
Thanks a lot! I will try to read that and clearfy my question as soon as possible!
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.