I have the following table that I generated using the table(data$a, data$b) function
a b c NA
d 0 45 42 63 0
e 0 12 45 63 0
f 0 95 65 21 0
NA 0 0 0 0 0
How can I remove the columns with " " and NA?
Here is a reproducible example
a b
a d
a d
a d
a d
a d
a d
a d
a d
a d
a d
a d
a d
b d
b d
b e
b e
b e
b e
c e
c e
c e
c e
c e
c e
c e
c e
c e
c e
c f
c f
c f
c f
c f
c f
c f
c f
c f
c f
c f
Note that there are no "" or NAs in the set, but they still appear in the table
In this table, both of the variables are factors.
Thank you!
It is possible that the NAs are character strings "NA" instead of NA, otherwise, the table would pick up with default useNA= "no" and remove it. One option is to change the values '' and "NA" to NA
df1[df1 == "NA"|df1 == ""] <- NA
Assuming that we have two column dataframe and all of the columns are character class
Update
If the dataset have "NA" or "", it would be a factor class column with unused levels already existing. One option is droplevels and then apply the table
table(droplevels(df1))
If we create a table called "mytable", you could try the following:
bad_cols <- which(colnames(mytable) == "NA" || colnames(mytable) == "")
mytable <- mytable[, -bad_cols]
This will first find the positions in which we either have NA or "" in the column, then we exclude it via subsetting and save it in the variable „mytable“ again.
Related
I got these two data frames:
a <- c('A','B','C','D','E','F','G','H')
b <- c(1,2,1,3,1,3,1,6)
c <- c('K','K','H','H','K','K','H','H')
frame1 <- data.frame(a,b,c)
a <- c('A','A','B','B','C','C','D','D','E','E','F','F','G','H','H')
d <- c(5,5,6,3,1,9,1,0,2,3,6,5,5,5,4)
e <- c('W','W','D','D','D','D','W','W','D','D','W','W','D','W','W')
frame2<- data.frame(a,d,e)
And now I want to include the column 'e' from 'frame2' into 'frame1' depending on the matching value in column 'a' of both data frames. Note: 'e' is the same for all rows with the same value in 'a'.
The result should look like this:
a b c e
1 A 1 K W
2 B 2 K D
3 C 1 H D
4 D 3 H W
5 E 1 K D
6 F 3 K W
7 G 1 H D
8 H 6 H W
Any sugestions?
You can use match to matching value in column 'a' of both data frames:
frame1$e <- frame2$e[match(frame1$a, frame2$a)]
frame1
# a b c e
#1 A 1 K W
#2 B 2 K D
#3 C 1 H D
#4 D 3 H W
#5 E 1 K D
#6 F 3 K W
#7 G 1 H D
#8 H 6 H W
or using merge:
merge(frame1, frame2[!duplicated(frame2$a), c("a", "e")], all.x=TRUE)
you can perform join operation on 'a' column of both dataframes and take those values only which are matched. you can do left join , and after that remove 'a' column from 2nd dataframe and also remove rest of the columns, which are'nt needed from 2nd dataframe.
Using dplyr :
library(dplyr)
frame2 %>%
distinct(a, e, .keep_all = TRUE) %>%
right_join(frame1, by = 'a') %>%
select(-d) %>%
arrange(a)
# a e b c
#1 A W 1 K
#2 B D 2 K
#3 C D 1 H
#4 D W 3 H
#5 E D 1 K
#6 F W 3 K
#7 G D 1 H
#8 H W 6 H
I have some data about the title of faculties in our college
I want to find all the titles and only list the ones that are unique, e.g. Professor, Assistant Teaching Professor, Instructor.
For instance. make "Distinguished Professor' and 'professor' have the same factor "professor"
Simply, it just like combine two factors to one factor
If I have a data set with 5 factor levels, I can change the levels by just renaming those columns:
set.seed(23)
x <- factor(sample(letters[1:5], 20, replace = TRUE))
x
# [1] c b b d e c e e e e e d b b e a c c e d
# Levels: a b c d e
levels(x)[3] <- "new_level"
x
# [1] a b b d e a e e e e e d b b e a a a e d
# Levels: a b d e
The number of levels will automatically be reduced, as shown.
I want to join two dataframes by two columns they have in common but I do not want mutual pairs to be considered as duplicates.
Sample dataframes look like:
>df
letter1 letter2 value
d e 1
c d 2
c e 4
>dc
letter1 letter2
a e
c a
c d
c e
d a
d c
d e
e a
I want to join them by the first two columns, leaving in the third column the value in df$value and NA if the row does not exist in df. I have tried:
s <- join(dc,df, by = c("letter1","letter2"))
>s
letter1 letter2 value
a e NA
c a NA
c d 2
c e 4
d a NA
d c 2
d e 1
e a NA
Here, the pair d c is considered the same as c d and the value in the third column is the same. What I want is d c being considered as non-present in df, so their row value is NA. My desired output is:
>s
letter1 letter2 value
a e NA
c a NA
c d 2
c e 4
d a NA
d c NA
d e 1
e a NA
How can I join the dataframes so mutual pairs are considered different combinations?
UPDATE: I am sorry but I have just realized there was a problem with my input dataframes and that the join line I was trying actually works. I will accept the first answer that also works to give credit to the author.
We can use apply to change the order
df[1:2] <- t(apply(df[1:2], 1, sort))
dc <- t(apply(dc, 1, sort)
and then do the join
You could use merge instead of join:
merge(dc,df, by = c("letter1","letter2"),all=TRUE)
#Creating the data frames
df <- data.frame(letter1=c("d","c","c"),
letter2=c("e","d","e"),
value=c(1,2,4))
dc <- data.frame(letter1=c("a","c","c","c","d","d","d","e"),
letter2=c("e","a","d","e","a","c","e","a"))
# Merging the data frames
dout <- merge(df,dc,by=c("letter1","letter2"),all=T)
# Outcome
letter1 letter2 value
1 c d 2
2 c e 4
3 c a NA
4 d e 1
5 d a NA
6 d c NA
7 a e NA
8 e a NA
I have this R data frame:
v1 <- LETTERS[1:10]
v2 <- LETTERS[1:4]
v3 <- LETTERS[4:5]
dat <- data.frame(cbind(v1,v2,v3))
v1 v2 v3
A A D
B B E
C C D
D D E
E A D
F B E
G C D
H D E
I A D
J B E
I would like to get a count of the number of occurrences of a given value (e.g. "A") for each column,
and save that as a new column in my data frame.
I my dataframe I want to calculate the occurrences af "A" in column v1 thru v3, and make a new column (CountA) with the count of A's.
My desired output would be:
v1 v2 v3 CountA
A A D 2
B B E 0
C C D 0
D D E 0
E A D 1
F B E 0
G C D 0
H D E 0
I A D 1
J B E 0
Try this:
dat$CountA <- rowSums(dat=="A")
I apologize, not sure how to insert a data.table into the question box.
I have a data set with a ton of rows like this:
phylum class family order genus species
A B C D E NA
A B C D E NA
A B C D NA NA
A B C D E F
A B C D NA NA
A B C D E F
I would like each matching row to be assigned a unique ID for example:
ID phylum class family order genus species
1 A B C D E NA
1 A B C D E NA
2 A B C D NA NA
3 A B C D E F
2 A B C D NA NA
3 A B C D E F
I have tried using GRP in a variety of ways but its not working.
For example:
DT2 = DT[,i:=.GRP,by=key(DT)]
I have looked at other samples but everything is assigning IDs based on a single or only 2 columns value and I want to use 6 different ones. Any help is greatly appreciated.
A solution with base R:
df2 <- unique(df)
df2$ID <- 1:nrow(df2)
merge(df, df2)
or using data.table:
dt[, ID := .GRP, by = names(dt)]