我正在阅读有关R的Kaggle教程,并尝试用magrittr
代替filter
。但这似乎不起作用,但我不明白为什么。他们似乎在做同样的事情。
我尝试了下面的代码。
f_countOfMen <- mutFoodData %>%
select(starts_with("gender")) %>%
filter(Gender == 1) %>%
rowSums(na.rm = T)
f_countOfWomen <- mutFoodData %>%
select(starts_with("gender")) %>%
filter(Gender == 2) %>%
rowSums(na.rm = T)
mutFoodData <- mutFoodData %>%
mutate(fMen = f_countOfMen, fWomen = f_countOfWomen) # add our new variables
# however it doesn't add the variables and produces an error
m_countOfMen <- mutFoodData %>%
select(starts_with("gender")) %>%
magrittr::equals(1) %>%
rowSums(na.rm = T)
m_countOfWomen <- mutFoodData %>%
select(starts_with("gender")) %>%
magrittr::equals(2) %>%
rowSums(na.rm = T)
mutFoodData <- mutFoodData %>%
mutate(mMen = m_countOfMen, mWomen = m_countOfWomen) # add our new variables
# this code does as expected
我希望添加新列,但我不断收到此错误:
mutate_impl(.data,点)中的错误: 结果大小错误(76),预期为124或1
答案 0 :(得分:1)
问题在于,在执行filter
时,行数减少了,并且正被添加到具有完整行的原始数据集中。在这里,创建filter
而不是rowSums
,获得library(dplyr)
mutFoodData %>%
mutate(fMen = rowSums(select(., starts_with("gender")) == 1, na.rm= TRUE),
fFemale = rowSums(2*(select(., starts_with("gender")) ==2), na.rm = TRUE))
的“男人”和“女人”
document.addEventListener('DOMContentLoaded', function () {
const checkbox = document.querySelector('.dark-mode-checkbox');
checkbox.checked = localStorage.getItem('darkMode') === 'true';
checkbox.addEventListener('change', function (event) {
localStorage.setItem('darkMode', event.currentTarget.checked);
});
});
答案 1 :(得分:1)
问题在于,在执行过滤器时,行数减少了,并且被添加到具有完整行的原始数据集中。
我没有意识到。
只需谷歌搜索行数函数,然后发现nrow
。所以我运行了这段代码
rowscount <- mutFoodData %>%
select(Gender) %>%
nrow()
rowscountFilter <- mutFoodData %>%
select(Gender) %>%
filter(Gender == 1) %>%
nrow()
rowscountMagittr <- mutFoodData %>%
select(Gender) %>%
magrittr::equals(1) %>%
nrow()
print(rowscount)
print(rowscountFilter)
print(rowscountMagittr)
结果是:
124
76
124
我现在明白了。谢谢。
答案 2 :(得分:0)
# A tibble: 6 × 64
GPA Gender breakfast calories_chicken calories_day calories_scone coffee
<chr> <int> <int> <int> <dbl> <dbl> <int>
1 3.654 1 1 610 3 420 2
2 3.3 1 1 720 4 420 2
3 3.2 1 1 430 3 420 2
4 3.5 1 1 720 2 420 2
5 2.25 1 1 610 3 980 2
6 3.8 2 1 610 3 420 2
# ... with 57 more variables: comfort_food <chr>, comfort_food_reasons <chr>,
# comfort_food_reasons_coded <int>, cook <dbl>, comfort_food_reasons_coded_1 <int>, cuisine <dbl>,
# diet_current <chr>, diet_current_coded <int>, drink <dbl>, eating_changes <chr>,
# eating_changes_coded <int>, eating_changes_coded1 <int>, eating_out <int>, employment <dbl>,
# ethnic_food <int>, exercise <dbl>, father_education <dbl>, father_profession <chr>,
# fav_cuisine <chr>, fav_cuisine_coded <int>, fav_food <dbl>, food_childhood <chr>, fries <int>,
# fruit_day <int>, grade_level <int>, greek_food <int>, healthy_feeling <int>, healthy_meal <chr>,
# ideal_diet <chr>, ideal_diet_coded <int>, income <dbl>, indian_food <int>, italian_food <int>,
# life_rewarding <dbl>, marital_status <dbl>, meals_dinner_friend <chr>, mother_education <dbl>,
# mother_profession <chr>, nutritional_check <int>, on_off_campus <dbl>, parents_cook <int>,
# pay_meal_out <int>, persian_food <dbl>, self_perception_weight <dbl>, soup <dbl>, sports <dbl>,
# thai_food <int>, tortilla_calories <dbl>, turkey_calories <int>, type_sports <chr>,
# veggies_day <int>, vitamins <int>, waffle_calories <int>, weight <chr>, food <int>, mMen <dbl>,
# mWomen <dbl>
@akrun和其他想要查看数据外观的人
csv格式的数据,包括标题行
GPA,Gender,breakfast,calories_chicken,calories_day,calories_scone,coffee,comfort_food,comfort_food_reasons,comfort_food_reasons_coded,cook,comfort_food_reasons_coded,cuisine,diet_current,diet_current_coded,drink,eating_changes,eating_changes_coded,eating_changes_coded1,eating_out,employment,ethnic_food,exercise,father_education,father_profession,fav_cuisine,fav_cuisine_coded,fav_food,food_childhood,fries,fruit_day,grade_level,greek_food,healthy_feeling,healthy_meal,ideal_diet,ideal_diet_coded,income,indian_food,italian_food,life_rewarding,marital_status,meals_dinner_friend,mother_education,mother_profession,nutritional_check,on_off_campus,parents_cook,pay_meal_out,persian_food,self_perception_weight,soup,sports,thai_food,tortilla_calories,turkey_calories,type_sports,veggies_day,vitamins,waffle_calories,weight
2.4,2,1,430,nan,315,1,none,we dont have comfort ,9,2,9,nan,eat good and exercise,1,1,eat faster ,1,1,3,3,1,1,5,profesor ,Arabic cuisine,3,1,rice and chicken ,2,5,2,5,2,looks not oily ,being healthy ,8,5,5,5,1,1,"rice, chicken, soup",1,unemployed,5,1,1,2,5,3,1,1,1,1165,345,car racing,5,1,1315,187
3.654,1,1,610,3,420,2,"chocolate, chips, ice cream","Stress, bored, anger",1,3,1,1,I eat about three times a day with some snacks. I try to eat healthy but it doesn't always work out that- sometimes eat fast food and mainly eat at Laker/ Egan ,2,2,I eat out more than usual. ,1,2,2,2,4,1,2,Self employed ,Italian,1,1,"chicken and biscuits, beef soup, baked beans",1,4,4,4,5,"Grains, Veggies, (more of grains and veggies), small protein and fruit with dairy ","Try to eat 5-6 small meals a day. While trying to properly distribute carbs, protein, fruits, veggies, and dairy. ",3,4,4,4,1,2,"Pasta, steak, chicken ",4,Nurse RN ,4,1,1,4,4,3,1,1,2,725,690,Basketball ,4,2,900,155
3.3,1,1,720,4,420,2,"frozen yogurt, pizza, fast food","stress, sadness",1,1,1,3,"toast and fruit for breakfast, salad for lunch, usually grilled chicken and veggies (or some variation) for dinner",3,1,sometimes choosing to eat fast food instead of cooking simply for convenience,1,3,2,3,5,2,2,owns business,italian,1,3,"mac and cheese, pizza, tacos",1,5,3,5,6,usually includes natural ingredients; nonprocessed food,i would say my ideal diet is my current diet,6,6,5,5,7,2,"chicken and rice with veggies, pasta, some kind of healthy recipe",2,owns business,4,2,1,3,5,6,1,2,5,1165,500,none,5,1,900,I'm not answering this.
3.2,1,1,430,3,420,2,"Pizza, Mac and cheese, ice cream",Boredom,2,2,2,2,"College diet, cheap and easy foods most nights. Weekends traditionally, cook better homemade meals ",2,2,Accepting cheap and premade/store bought foods,1,3,2,3,5,3,2,Mechanic ,Turkish ,3,1,"Beef stroganoff, tacos, pizza",2,4,4,5,7,"Fresh fruits& vegetables, organic meats ","Healthy, fresh veggies/fruits & organic foods ",2,6,5,5,2,2,"Grilled chicken
Stuffed Shells
Homemade Chili",4,Special Education Teacher,2,1,1,2,5,5,1,2,5,725,690,nan,3,1,1315,"Not sure, 240"
3.5,1,1,720,2,420,2,"Ice cream, chocolate, chips ","Stress, boredom, cravings ",1,1,1,2,I try to eat healthy but often struggle because of living on campus. I still try to keep the choices I do make balanced with fruits and vegetables and limit the sweats. ,2,2,I have eaten generally the same foods but I do find myself eating the same food frequently due to what I have found I like from egan and the laker. ,3,4,2,2,4,1,4,IT,Italian ,1,3,"Pasta, chicken tender, pizza ",1,4,4,4,6,"A lean protein such as grilled chicken, green vegetables and brown rice or other whole grain ",Ideally I would like to be able to eat healthier foods in order to loose weight. ,2,6,2,5,1,1,"Chicken Parmesan, Pulled Pork, Spaghetti and meatballs ",5,Substance Abuse Conselor,3,1,1,4,2,4,1,1,4,940,500,Softball,4,2,760,190
2.25,1,1,610,3,980,2,"Candy, brownies and soda.","None, i don't eat comfort food. I just eat when i'm hungry.",4,3,4,nan,My current diet is terrible. I barely have time to eat a meal in a day. When i do eat it's mostly not healthy.,2,2,Eating rice everyday. Eating less homemade food.,1,3,1,3,4,2,1,Taxi Driver,African,6,3,"Fries, plaintain & fried fish",1,2,2,2,4,"Requires veggies, fruits and a cooked meal. ",My ideal diet is to eat 3 times a day including breakfast on time. Eat healthy food. ,2,1,5,5,4,2,Anything they'd want. I'd ask them before hand what they want to eat and it depends on which type of friend is coming.,1,Hair Braider,1,1,2,5,5,5,1,2,4,940,345,None.,1,2,1315,190
3.8,2,1,610,3,420,2,"Chocolate, ice cream, french fries, pretzels","stress, boredom",1,2,1,1,"I eat a lot of chicken and broccoli for dinner, and usually tuna sandwiches for lunch.",3,1,I started eating a lot less and healthier because I wasn't playing sports year round anymore.,2,5,2,3,5,1,4,Assembler ,Thai,4,1,"grilled chicken, hamburgers",1,4,4,5,4,"Protein, vegetables, fruit, and some carbs","I would ideally like to eat more fresh fruits and vegetables. However, its difficult to get to the store all the time to buy fresh.",2,4,5,5,8,1,"Grilled chicken, steak, pizza",4,Journalist,4,2,2,2,5,4,1,1,5,940,690,soccer,4,1,1315,180
3.3,1,1,720,3,420,1,"Ice cream, cheeseburgers, chips.","I eat comfort food when im stressed out from school(finals week), when I`m sad, or when i am dealing with personal family issues.",1,3,1,1,"I eat a very healthy diet. Ocassionally, i will eat out and get unhealthy food.",1,2,"Freshmen year i ate very unhealthy, but now it is much healthier because of self control.",2,5,2,2,2,2,3,Business guy,Anything american style.,5,1,"chicken, cheesey potatoes, and hot dogs",1,5,2,3,3,A healthy meal has a piece of meat followed by a lot of fruit and veggies,My ideal diet is filled with a lot of fruit and chicken. I also really enjoy eggs any type of way with toast.,2,5,1,3,3,1,"chicken, steak, pasta ",2,cook,4,1,1,5,1,3,1,2,1,725,500,none,4,2,1315,137
3.3,1,1,430,nan,420,1,"Donuts, ice cream, chips",Boredom ,2,3,2,1,I eat whatever I want in moderation.,1,1,I snack less,2,8,5,2,5,nan,5,High School Principal,Seafood,1,3,"Shrimp, spaghetti",1,4,1,5,7,Colorful,The same as it is now.,6,5,5,5,8,2,"Pasta, Fish, Steak",5,Elementary School Teacher,2,1,2,3,5,4,2,2,5,725,345,none,3,2,760,180
3.3,1,1,430,3,315,2,"Mac and cheese, chocolate, and pasta ","Stress, anger and sadness ",1,3,1,1,"I eat healthy all the time when possible. I treat myself occasionally. I don't really like the greasy meals, if anything I would eat sweets over the greasy meals. ",1,1,I cook a lot of my own foods back at home so not being able to cook my own healthy choices. I eat more carbs than normal when I'm at college due to the choices given in the cafe. ,1,3,3,3,5,1,5,commissioner of erie county ,Italian ,1,1,"Pasta, Eggs, Pancakes ",1,5,1,5,3,Chicken and rice with a side of veggies. ,"Lots of protein, carbs, and fruits and veggies. ",2,4,4,5,3,2,pasta salad and bread,5,Pharmaceutical rep,5,1,3,3,4,3,1,1,4,580,345,field hockey,5,1,900,125
3.5,1,1,610,3,980,2,"Pasta, grandma homemade chocolate cake anything homemade ",Boredom ,2,1,2,1,i eat very healthy. Maybe eat out 1-2 times a week.,1,2,Nun ,3,4,2,1,5,1,2,Idk,Orange chicken and chow mani noodles ,4,1,Chicken,1,5,3,1,9,"Chicken, and veggies ","Chicken, fish ",7,3,1,5,8,1,"chicken al king, spaghetti, fish",4,Chidos Cleaners,2,3,1,2,2,1,1,1,2,940,345,soccer,5,2,900,116
3.904,1,1,720,4,420,2,"chocolate, pasta, soup, chips, popcorn","sadness, stress, cold weather",3,3,3,1,"I am very health concious. I eat many fruits, veggies, and protiens. ",1,1,Less meat. ,4,5,1,2,5,1,3,Home Marker ,Italian,1,1,Scalloped potatoes and ham ,1,5,2,5,1,"lean protein, veggies, fruit, complex carbs",More healthy varienty. ,2,5,5,5,1,2,"Chicken parm, Fish, Pasta dishes ",4,Court Reporter ,5,1,1,3,5,2,1,1,5,940,500,Running,5,1,900,110
3.4,2,1,430,3,420,2,"Cookies, popcorn, and chips","Sadness, boredom, late night snack ",3,5,3,1,I focus mostly on proteins and fruits. I eat a lot less vegetables and a few carbohydrates.,1,2,I have been eating a lot more salads and soups.,2,5,1,3,4,3,3,Shirt designer ,Chinese ,4,3,"Pizza, chicken wings, and corn on the cob ",1,4,1,3,9,A salad with a reasonable amount of dressing and a meat.,My ideal diet would be the proper portions of all food categories. I would also like to eat proteins around my workouts and avoid all deserts.,1,5,3,4,9,2,"Cereal, pizza, toast ",4,Child care provider ,2,1,2,2,3,5,2,1,3,940,500,Soccer and basketball ,3,2,575,264
3.6,1,1,610,3,420,2,"ice cream, cake, chocolate","stress, boredom, special occasions",1,2,1,1,Not as healthy when I am at school compared to home. Still very aware of the nutrition I am taking in.,2,2,not as healthy because healthy food goes bad quickly and it is expensive,1,3,4,2,5,2,2,business owner,italian,1,1,"steak, lasagna, crab",1,5,3,4,8,Lots of vegetabls with some grains like rice. Also has lean meat such as fish or chicken.,organic/healthy. fruits and vegetables but still protein to give sustainance. ,2,5,3,5,10,2,"pasta, chicken, steak",4,business owner,2,2,2,3,3,3,1,1,5,1165,850,intramural volleyball,5,2,1315,123
3.1,2,1,610,3,420,2,"Pizza, fruit, spaghetti, chicken and Potatoes ","Friends, environment and boredom",2,3,2,1,"Making sure I have enough protein intake, as well with fibre, vitamins and carbs. ",1,2,"I knew I would eat alot my freshmen year, before coming to college i had a diet plan. ",3,4,2,3,4,2,4,Commidity trader ,Chinese ,4,3,Pizza,1,5,3,4,2,Green and not greasy,"Eat a little less than I usually do, and intaking more protein. ",1,5,2,3,1,1,"Pizza, chicken and rice, roast beef.",4,Charity worker,2,2,2,3,1,3,1,1,4,940,500,Hockey,5,1,900,185
nan,2,2,430,nan,980,2,"cookies, donuts, candy bars",boredom,2,4,2,1,I like a lot of foods that arent home cooked or healthy for you.,2,2,none,3,4,4,3,1,1,5,Hockey Coach,italian food,1,2,pizza,1,3,1,1,6,"chicken, veggies, rice",I wish I ate lots of healthy foods like veggies and salads. ,2,4,1,5,nan,2,pizza buffalo chicken pasta,4,Librarian,1,1,2,3,1,2,2,1,1,940,345,Hockey,1,2,1315,180
4,1,1,265,3,420,1,"Saltfish, Candy and Kit Kat ",Stress ,1,3,1,nan,I eat very healthy ,1,1,More Water ,2,5,1,2,2,2,1,Construction ,Jamaican ,7,1,"Curry, Stew Chicken and Saltfish",1,5,1,2,7,"Not too much carbs, a lot of protein, healthy fats and fruits and vegs ",Vegetables and Fruits with Meat ,2,1,5,3,9,2,"Curry goat, saltfish, jerk chicken ",2,Police,4,1,2,2,2,3,1,2,1,580,345,nan,5,1,760,145
3.6,2,1,430,3,980,2,"chips, cookies, ice cream","I usually only eat comfort food when I'm bored, if i am doing something, i can go for hours without eating ",2,3,2,1,My current diet is eggs and a meat every morning for breakfast. For lunch ill usually have some pasta or rice with a meat and sometimes a side of veggies. for dinner i usually eat less carbs and more protein and veggies. ,3,2,"I would say i just eat a little more, but not in a bad way. I have unlimited swipes at the cafe so instead of snacking on chips before lunch or dinner i will get something from the cafe.",1,2,2,3,4,1,2,self employed construction ,American or Italian ,5,3,"chicken fingers, steak, mac and cheese",1,3,1,3,8,"for me usually a big piece of chicken or steak with a side of veggies, and i usually have rice with mine as well","I like how my diet it now. However, after my season i am going to cut back on some carbs because i won't be working out as much.",1,6,3,5,7,2,Grilled chicken or steak with veggies and rice. or some type of pasta and chicken ,2,stay at home mom,4,1,1,6,2,2,1,1,3,940,500,hockey,4,2,900,170
3.4,1,1,720,3,980,1,"Chocolate, ice crea ","Sadness, stress",3,3,3,1,I eat lots of fruit and veggies. I eat lots of chicken.,1,1,I ate at the food hall.,4,6,3,2,1,3,5,Engineer,Chicken ,5,3,Chicken,1,5,2,3,6,Everything from food group ,Lots of fruit and veggies. Lots of chicken.,2,5,1,5,9,2,"Spaghetti, Chicken, Steak",3,Daycare provider,2,1,1,2,1,3,2,2,1,1165,690,dancing ,5,1,1315,135
2.2,2,1,430,2,420,2,"pizza, wings, Chinese","boredom, sadness, hungry",2,4,2,1,Current diet right now isn't very good. I eat at the school's cafeteria and they do not have the best choices.,2,2,None really,3,4,2,3,3,2,5,architect,Italian,1,1,manacotti,1,2,1,3,4,"lots of fruits and vegitibles, not any fried foods",Something that tastes good and also is good for you. This is very hard to find though.,3,5,2,5,7,1,"chicken, manicotti, rice",4,physical therapist,1,1,1,3,3,4,1,nan,3,940,345,basketball,2,2,900,165
3.3,2,1,610,3,980,2,"Fast food, pizza, subs","happiness, satisfaction",7,5,7,1,"I eat 2 meals a day, lunch and dinner",2,2,Late night food,1,3,4,2,4,1,5,CFO,Mexican,2,3,"pizza, chicken fingers",1,2,2,2,5,"Rice, Meat, vegetable",3 smaller meals a day,1,6,1,4,2,1,"Chicken, Pasta, Veal",4,Teacher,2,1,1,2,1,6,1,1,1,1165,850,Soccer,3,2,1315,175
3.87,2,1,610,3,315,1,"chocolate, sweets, ice cream",Mostly boredom,2,3,2,3,"Random. Not strict diet, changes during season",2,nan,"Less money, less food :(",1,8,1,2,3,1,4,European logistics director ,Indian,8,2,Pasta,1,3,4,5,8,Green and colourful,"Mix of Mexican, Asian, and Italian foods.",7,6,5,5,7,2,"Meat, wine, chocolate pudding",2,House wife,4,1,3,5,5,2,2,1,5,725,500,Tennis,2,2,900,195
3.7,2,1,610,3,420,1,"burgers, chips, cookies","sadness, depression ",3,5,3,2,balanced ,1,1,got worse ,1,3,1,2,3,1,4,accountant ,italian ,1,1,pasta,2,4,1,4,2,"pasta. veg, water ","fruit, veg and protien ",2,6,5,5,5,1,"pasta, pizza ",4,Underwiriter ,3,1,3,5,2,2,1,1,4,940,850,tennis soccer gym,3,1,1315,185
3.7,2,2,610,3,420,2,"Chilli, soup, pot pie",Stress and boredom ,1,4,1,1,2 meals a day. Rarely eat snacks. Fairly balanced diet. ,1,1,Drink coffee all the time. ,1,7,2,2,5,3,4,Commercial Real Estate,Asian ,4,1,Chicken Nuggets,1,3,3,2,4,"Protein, vegetables, grains ",3 healthy meals a day. No caffiene. ,1,4,4,5,5,1,"Pizza, Pasta, Poutine",2,Beautician ,2,1,2,3,2,3,1,1,4,940,690,Gaelic Football,4,1,1315,185
3.9,1,1,720,2,420,2,"Soup, pasta, brownies, cake","A long day, not feeling well, winter ",6,1,6,1,I eat a lot of chicken and try to cook my own meals as much as possible using a lot of rice and vegetables.,1,2,"I watch the amount of ""snacks"" I eat more than I did in high school.",1,2,4,3,4,nan,3,Manager at Pepsi,Chinese,4,1,Spaghetti ,1,4,3,5,5,A healthy meal is a lean meat with mostly vegetables and a starch like brown rice or quinoa.,I would like to be a person to eat a larger vegetable based diet with little to no processed products in my diet especially sugar and salt.,2,4,3,5,2,2,"Pasta, chicken and rice, and soup",2,Unemployed,2,1,1,3,3,4,2,2,4,940,500,none,4,2,1315,105
2.8,1,2,720,3,420,2,"chocolate, ice cream/milkshake, cookies",boredom,2,3,2,1,"vegetarian, eat breakfast some days before class, can eat a lot of junk food so I refrain from allowing myself to buy any",1,1,"none, I have dormed since high school",3,4,2,3,2,1,4,VP of ,don't have one,0,3,"Chicken parm, beef stroganof, tacos",1,4,1,3,8,"good portions, lots of color, targets main food groups","healthy, colorful, tasty, room for desserts",2,6,3,3,5,1,"Pasta
Take out",3,Medical biller,4,1,1,3,3,3,1,1,3,1165,690,Ice hockey,3,2,760,125