Logziii
February 13, 2020, 8:41am
1
Hi,
I am beginner in R and want to know how to check single value from data frame ab with multiple columns of data frame bc.
ab <- data.frame(f1 = c("Single","Double","Triangle"),
f2 = c("12","15","18"))
bc <- data.frame(f1 = c("Double","Triangle","Square",NA),
f2 = c("Square","Double","Triangle","Hexagon"),
f3 = c("Single",NA,NA,"Triangle"),
f4 = c("Square","Double","Triangle","Multi"),
f5 = c("Super","Double","Nano","NA"),
f6 = c("Names","Surname","Triangle","NA"))
From dataframe ab, want to check ,if ab$f1 value is in dataframe bc any column [f1:f6] ,if it is there
mutate new column in ab with 1 else 0.
I would like to understand How it works in the for loop.
Thanks in advance.
nwerth
February 13, 2020, 2:55pm
2
Can you show what you expect the output would be for this example?
Also, I've added some formatting to the code in your post. In general, you can do the same by adding three backticks before and after your code. So, in the post editing box, you would type:
Blah blah blah
```
x <- 1 + 2
```
Yaddah yaddah
And this would show up as:
Blah blah blah
x <- 1 + 2
Yaddah yaddah
You can read more about it here:
Formatting Code: the Basics
All code or console output you include in your posts should be formatted properly. Luckily, this is very easy to do! Just use the code formatting button at the top of the post editing box:
[image]
Select some code
Click the </> button!
The code formatting button automatically adds special Markdown formatting symbols to whatever you selected. If you check the preview pane to the right of your post, you'll see that the forum software now outputs nicely formatted code .
[04]
The code formatting button is pretty smart! It can tell the difference between snippets of code that are part of a sentence and blocks of code that stand alone. …
1 Like
FJCC
February 13, 2020, 3:46pm
3
Rather than use a for loop, I would make a vector out of bc.
ab <- data.frame(f1 = c("Single","Double","Triangle"),
f2 = c("12","15","18"))
bc <- data.frame(f1 = c("Double","Triangle","Square",NA),
f2 = c("Square","Double","Triangle","Hexagon"),
f3 = c("Single",NA,NA,"Triangle"),
f4 = c("Square","Double","Triangle","Multi"),
f5 = c("Super","Double","Nano","NA"),
f6 = c("Names","Surname","Triangle","NA"))
bcVec <- unlist(bc) #makes a vector from the values in bc
ab$InBC <- as.numeric(ab$f1 %in% bcVec)
ab
#> f1 f2 InBC
#> 1 Single 12 1
#> 2 Double 15 1
#> 3 Triangle 18 1
Created on 2020-02-13 by the reprex package (v0.2.1)
1 Like
does this new question relate to your previous question ?
It seems structurally quite similar.
I am a beginner in R and want to know how it will works for each row.
Eg.
I have a two data frames.
I want to see if the rows exist in the other multiple columns of other data frame.
I want to loop every row in ab$Fruits and check if each name is somewhere in another dataframe bc[E1:E3]columns.
ab<-data.frame(Fruits = c("Apple","Banana"),Units = c("3","2"))
bc<-data.frame("E1"=c("Apple","Grapes","Watermelon","Na"),
"E2"=c("Grapes","Berries","Custard","Guava"),
"E3"=c("Apple","Banana","Grapes","Na"))
eg.
I want to do with loops or any function.When the loops starts, first row of ab$fruits match with each rows of bc,if first row (Apple) mat…
system
Closed
March 5, 2020, 5:22pm
5
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.