Please help make R read the file into a dataframe, not a list. I am trying the following code:
head <- c("y", "x", "Label", "NDAI", "SD", "CORR","DF", "CF", "BF", "AF", "AN")
data1 <- read.delim("image1.txt", sep = "", header = T, col.names = head)
typeof(data1)
But this had no effect.
Related
I am working on a project at work where I am required to compare two Excel spreadsheets to see if a title has been added or removed from the second excel sheet (Book 2). The code I have created works well, however, it is difficult to interpret for someone without R experience and does not look great.
My goal is to compare two Excel spreadsheets and output the differences in a clear fashion so that my colleagues can understand and change the differences. Optimally, it would output a table that presents the information in a way that it can be easily located and changed. I appreciate all the help.
structure(list(Title = c("D", "Mortal Kombat", "Godzilla",
"Wonder", "Suicide Squad", "Mulan"), Studio = c("X", "X",
"X", "X", "X", "Y"), Type = c("Special", "Special", "Special",
"Special", "Special", "Special")), class = c("tbl_df", "tbl",
"data.frame"), row.names = c(NA, -6L))
structure(list(Title = c("D", "Mortal Kombat", "Godzilla",
"Wonder", "Trolls"), Studio = c("X", "X", "X", "X", "X"
), Type = c("Special", "Special", "Special", "Special", "Special"
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-5L))
library("compareDF")
library("readxl")
dat <- read_xlsx("Book1.xlsx")
dat2 <- read_xlsx("Book2.xlsx")
compare_df(dat, dat2)
This is certainly not elegant, but it should be functional with a readable output:
library(tidyverse)
dat <- read_xlsx("Book1.xlsx")
dat2 <- read_xlsx("Book2.xlsx")
book1_output <- anti_join(dat,dat2, by = "Title") %>% mutate(source = "Book1")
book2_output <- anti_join(dat2,dat, by = "Title") %>% mutate(source = "Book2")
final_output <- rbind(book1_output,book2_output)
I'm making a table using formattable and I'd like to hide the column titles for the descriptive columns.
So for example, for dataframe "df" I'd like the resulting table to only show the column titles for msmt1, 2, and 3 and then have blank title names for the "site" and "variable" columns.
library(formattable)
df<-data.frame("site" = rep(c("1", "2"), 3),
"variable" = c("C", "C", "O", "O", "N", "N"),
"msmt1" = runif(6),
"msmt2" = runif(6),
"msmt3" = runif(6))
formattable(df)
Replacing the df column names with " " makes them show up as "X." in the table.
Is this possible in formattable?
How about this:
formattable(df, col.names = c("","","msmt1", "msmt2", "msmt3"))
In case you have more columns and don't want to specify them all literally, you could make it more dynamic like this:
formattable(df, col.names = c(rep("", 2), colnames(df)[3:ncol(df)]))
I have a data frame with one column X. In addition, I have an object (ob) class (character). I would like to create a new object (ob1) were characters in ob that match df content are replaced with the word "word".
df
X
1 ABC
2 ACC
3 ATT
ob
[1] "ABC", "ACC", "ATT", "AGG", "ACT"
result
ob1
[1] "word", "word", "word", "AGG", "ACT"
Combining ifelse with %in% should do the job:
ob <- c("ABC", "ACC", "ATT", "AGG", "ACT")
df <- data.frame(x = c("ABC", "ACC", "ATT"))
ifelse(ob %in% df$x, yes = "word", no = ob)
Here is an approach with stringi:
library(stringi)
sapply(ob,function(x)stri_replace_all_fixed(x,df$X,"word",vectorize_all = FALSE))
ABC ACC ATT AGG ACT
"word" "word" "word" "AGG" "ACT"
Data:
df<-structure(list(X = c("ABC", "ACC", "ATT")), class = "data.frame", row.names = c("1",
"2", "3"))
ob<-c("ABC", "ACC", "ATT", "AGG", "ACT")
I have the following dataset:
Name Score
Amy A
Bob B
Charlie C
I want to generate three PDF files:
First one: Amy.pdf, with the content "Amy, your score for MATH 101 is A."
Second one: Bob.pdf, with the content "Bob, your score for MATH 101 is B."
Third one: Charlie.pdf, with the content "Charlie, your score for MATH 101 is C."
Is there a simple to way to do this with only one R or R markdown file? There are hundreds of individuals in the actual dataset.
One way to do it is to create it as a graphic and export it as a pdf:
Data:
d <- structure(list(Name = structure(1:3, .Label = c("Amy", "Bob",
"Charlie"), class = "factor"), Score = structure(1:3, .Label = c("A",
"B", "C"), class = "factor")), class = "data.frame", row.names = c(NA,
-3L))
Perform operation:
for(i in 1:nrow(d)){
pdf(file = paste0(d[i, "Name"], ".pdf"))
plot(0:1, 1:0, bty = "n", type = "n", axes = F, xlab = "", ylab = "")
text(.5, .5, paste0(d[i, "Name"], ", your score for MATH 101 is ", d[i, "Score"]))
dev.off()
}
I want to split the Out column of Test data.frame into columns separating based on blank space. Here is my MWE. I tried separate function from tidyr package and strsplit function from base R but couldn't figured out the problem.
Test <-
structure(list(Out = structure(1:2, .Label = c("t1* -0.4815861 0.3190424 0.2309631",
"t2* 0.9189246 -0.1998455 0.2499412"), class = "factor")),
.Names = "Out", row.names = c(NA, -2L), class = "data.frame")
library(dplyr)
library(tidyr)
Test %>% separate(Out, c("A", "B", "C", "D"), sep = " ")
Error: Values not split into 4 pieces at 1, 2
strsplit(Test$Out, " ")
Error in strsplit(Test$Out, " ") : non-character argument
try
Test %>% separate(Out, c("A", "B", "C", "D"), sep = "\\s+")
which allows for multiple spaces (\\s+).