Say, I have a data table with three columns: apples, orange, and age.
What code I can write in R to make the other one with upper case: FRUITS, AGE, USE
apple
orange
age
FRUITS
AGE
USE
3
2
1-3
-
-
apple
1-3
3
4
5
4-6
-
-
apple
4-6
4
8
9
7-9
-
-
apple
7-9
8
-
-
orange
1-3
2
-
-
orange
4-6
5
-
-
orange
7-9
9
This is an example so I gives fewer values, but let's say my data have 30 rows like that. I do not want to manually add each rows into a new data frame. how can I turn the apples and oranges into FRUITS and make a column use?
I think using the pivot_longer() tidy function moodymudskipper suggested it would be coded like this
library(tidyr)
new_data <- data %>%
pivot_longer(!age, names_to = "FRUITS", values_to = "USE")%<%
select_all(toupper)
One possible way to solve your problem
library(data.table)
melt(as.data.table(df),
measure=c("apple", "orange"),
variable.name="FRUITS",
value.name="USE")
I am having trouble finding a way to cleaning update the amount column in table 1 with the price column in table 2. I know that left_join and merge could be used to join the price column, rename it, and then drop it, but I am wondering if there is simpler way to avoid creating a mess.
I should state that the real dataset is more complicated and that the amount column in table 1 needs to be conditionally updated somehow based on table 2.
Table 1
Fruit
Vegetable
amount
apple
broccoli
pear
spinach
pineapple
carrot
Table 2
Fruit
Vegetable
price
apple
broccoli
10
pear
spinach
5
pineapple
carrot
2
If you don't want to use merge and update process you can use match.
table1$amount <- table2$price[match(paste(table1$Fruit, table1$Vegetable),
paste(table2$Fruit, table2$Vegetable))]
I have been trying to read a CSV into R. The CSV is separated in a strange way with all values within one column separated by commas like in this picture. The top row is the column names and then below are the values
When I try read_csv("filename") nothing shows up in the tibble except a bunch of NA values like in this picture after running the view function . How can I approach this?
Here is the data for reference
, Calories, Fat (g), Carb. (g), Fiber (g), Protein (g)
Chonga Bagel,300,5,50,3,12
8-Grain Roll,380,6,70,7,10
Almond Croissant,410,22,45,3,10
Apple Fritter,460,23,56,2,7
Banana Nut Bread,420,22,52,2,6
Blueberry Muffin with Yogurt and Honey,380,16,53,1,6
Blueberry Scone,420,17,61,2,5
Butter Croissant,240,12,28,1,5
Butterfly Cookie,350,22,38,0,2
Cheese Danish,320,16,36,1,8
Chewy Chocolate Cookie,170,5,30,2,2
Chocolate Chip Cookie,310,15,42,2,4
Chocolate Chunk Muffin,440,21,60,2,7
Chocolate Croissant,330,18,38,1,6
Chocolate Hazelnut Croissant,390,22,43,2,7
Chocolate Marble Loaf Cake,490,24,64,2,6
Cinnamon Morning Bun,390,15,56,2,8
Cinnamon Raisin Bagel,270,1,58,3,9
Classic Coffee Cake,390,16,57,1,5
Cookie Butter Bar,360,23,36,0,2
Use the following code to read the data
df = read.csv("starbucks-menu-nutrition-food.csv", skipNul = T)
head(df, 2)
ÿþ Calories Fat..g. Carb...g. Fiber..g. Protein..g.
1 Chonga Bagel 300 5 50 3 12
2 8-Grain Roll 380 6 70 7 10
Then you may consider renaming the columns like for e.g.
colnames(df) <- c("Food", "Calories", "Fat", "Carb", "Fiber", "Protein")
for further processing of the data.
I want to visualize a dynamic network using the ndtv package in R.
My dataset (data) looks like this:
0 1 Apple Banana
0 1 Peach Banana
0 1 Apple Strawberry
1 2 Apple Banana
1 2 Apple Peach
2 3 Banana Peach
…
So the columns are onset, terminus, tail, head.
If I want to create a networkDynamic object from this list by
nw <- networkDynamic(edge.spells=data)
I get an error saying "the tail column of the edge.spells argument to networkDynamic must be a numeric vertex id". So I guess I need to convert those strings into numeric values. How do I do that? And if I do that, how do I keep the names? I don't want a network that just displays the numeric IDs of those names, I want to see those names in the network.
I couldn't find any useful information by searching the web, and this tutorial doesn't show what I want to do. I would've liked to see how they actually constructed the short.stergm.sim data instead of just using it.
Any help is very much appreciated!
I found a way to map ids to the names.
names <- unique(c(data$head,data$tail))
data$head <- match(data$head,names)
data$tail <- match(data$tail,names)
And then I could create the networkDynamic object
nw <-networkDynamic(edge.spells=data)
and add the names to the network
network.vertex.names(nw) <- names
This post helped me a lot.
I've found a number of answers to my question that almost get me to the result I want, but not quite!
I've got two data sets that include word lists, something like:
df1:
Word | Speaker
apple 1
dog 1
lobster 1
tree 2
df2:
Word | Speaker
car 2
lobster 2
fish 1
bird 1
I want to create a new column in df1 that will tell me whether or not the same word appears in df2, regardless of exactly where in the list it occurs and who the speaker was. So I want to create a new column in df1, similar to this:
df1
Word | Speaker | Match
apple 1 FALSE
dog 1 FALSE
lobster 1 TRUE
tree 2 FALSE
It seems that it should be very easy but I can't quite get it to do the right thing. Any help much appreciated!
You're right - it is easy! You need %in%...
df1$Match <- (df1$Word %in% df2$Word)