charts.PerformanceSummary() not working for intraday data - r

I am trying to plot intraday data using PerformanceAnalytics::charts.PerformanceSummary()
but I get the following error message:
charts.PerformanceSummary(e[,1:10])
Error in as.POSIXlt.POSIXct(.POSIXct(.index(x)), tz = indexTZ(x)) :
invalid 'tz' value
It seems that charts.PerformanceSummary only takes daily data but not intraday?
Can someone please come up with a solution for this?
e <- structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0.000369303493611195, 0,
0, 0.000590667454223315, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.000150829562594268,
0.000150806816467952, -0.00015078407720126, -0.000150806816468174,
0.000301659125188536, 0, 0, -0.000617817867292869, 0, 0, 0, 0,
0.000107944732297138, 0.000323799244468459, -0.000215796288303927,
0, 0.000215842866393423, 0, 0, 0, 0, 0, 0), class = c("xts",
"zoo"), .indexCLASS = c("POSIXlt", "POSIXt"), tclass = c("POSIXlt",
"POSIXt"), .indexTZ = c("America/New_York", "EST", "EDT"), tzone = c("America/New_York",
"EST", "EDT"), index = structure(c(1496755860, 1496755920, 1496755980,
1496756040, 1496756100, 1496756160), tzone = c("America/New_York",
"EST", "EDT"), tclass = c("POSIXlt", "POSIXt")), .Dim = c(6L,
10L), .Dimnames = list(NULL, c("AADR", "AAXJ", "ACIM", "ACSI",
"ACTX", "ACWF", "ACWI", "ACWV", "ACWX", "ADRA")))

The problem is the POSIXlt index. You should convert it to POSIXct, which is most easily accomplished by creating a new xts object. Try the commands below.
e <- xts(coredata(e), as.POSIXct(index(e)))
PerformanceAnalytics::charts.PerformanceSummary(e[,1:10])
The chart throws an error with your example data, after drawing the first plot:
Error in segments(xlim[1], y_grid_lines(ylim), xlim[2], y_grid_lines(ylim), :
cannot mix zero-length and non-zero-length coordinates
But that may be because there aren't enough observations. Let me know if it doesn't work on your actual data and I'll investigate further.

Related

NMDS does not work with specific data set

Im currently trying to run an NMDS with two subsets of a data set: metalimnion and hypolimnion. If i seperately subset the metalimnion data and run the NMDS it works fine but if i include the hypolimnion data i get errors saying that the data is insufficient:
for(i in 1:10){
tmp<-metaMDS(dSS.scree, k=i, autotransform=FALSE)
dimension[i,1]<-i
dimension[i,2]<-tmp$stress}
Warning messages:
1: In metaMDS(dSS.scree, k = i, autotransform = FALSE) :
stress is (nearly) zero: you may have insufficient data
Here's a portion of the data since its quite large:
structure(list(repID = c("WE1_1_hypolimnion_2018-06-19_Control",
"WE1_1_hypolimnion_2018-07-09_Control", "WE1_1_hypolimnion_2018-08-20_Control",
"WE1_1_hypolimnion_2018-10-22_Control", "WE1_1_hypolimnion_2019-05-29_Impact",
"WE1_1_hypolimnion_2019-06-18_Impact"), Chironomidae = c(0.00976189612651521,
0, 0.00981483562351682, 0, 0.210840654066634, 0), Hydrachnidia = c(0,
0, 0, 0, 0, 0), Megaloptera = c(0, 0, 0, 0, 0, 0), Sphaeriidae = c(0,
0, 0, 0, 0, 0), Chaoborus = c(0, 0.0266585776800614, 0.034725300150823,
0.142770363360079, 0, 0.036042564578147), Tubifex = c(0, 0, 0,
0, 0, 0), Trichoptera = c(0, 0, 0, 0, 0, 0), Gastropoda = c(0,
0, 0, 0, 0, 0), Ceratopogonidae = c(0, 0, 0, 0, 0, 0), Ephemeroptera = c(0,
0, 0, 0, 0, 0), `Diptera Tabanidae` = c(0, 0, 0, 0, 0, 0), `Coleoptera Dytiscidae` = c(0,
0, 0, 0, 0, 0), `Odonata Anisoptera` = c(0, 0, 0, 0, 0, 0), `Unidentified Hemiptera` = c(0,
0, 0, 0, 0, 0), lakeID = c("WE1", "WE1", "WE1", "WE1", "WE1",
"WE1"), depth = c("hypolimnion", "hypolimnion", "hypolimnion",
"hypolimnion", "hypolimnion", "hypolimnion"), BACI = c("Control",
"Control", "Control", "Control", "Impact", "Impact"), date = structure(c(1529366400,
1531094400, 1534723200, 1540166400, 1559088000, 1560816000), class = c("POSIXct",
"POSIXt"), tzone = "UTC")), row.names = 20:25, class = "data.frame")
Both datas combined is larger than each seperately yet I get this error. Any idea as to why?

How can I summarize several timesteps in R?

I want to draw a heat map showing the operating period of a ventilation system over the year. Since my dataset has 1minute-timesteps, I need to summarize the values to hourly-timesteps (1440 values on the y-axis result in a too small resolution). So I am looking for a command to average the first 60 values, the next 60 and so on...
dput(head(mydate,20))
structure(list(date = structure(c(1498373340, 1498373400, 1498373460,
1498373520, 1498373580, 1498373640, 1498373700, 1498373760, 1498373820,
1498373880, 1498373940, 1498374000, 1498374060, 1498374120, 1498374180,
1498374240, 1498374300, 1498374360, 1498374420, 1498374480), class = c("POSIXct",
"POSIXt"), tzone = ""), DS.ZV_SB = c(0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, 20L), class = "data.frame")

Filtering list of list values in R without using for

I am translating looping for() operations into apply() family ones: (sapply, lapply, mapply, etc.) to gain performance in all my R code. I have a list of lists named lt with the following look:
lt
$`11141550000`
y
2016-02-05 18
2016-03-03 8
2016-03-30 6
2016-04-26 0
$`11140780000`
y
2016-03-25 2
2016-03-30 0
2016-04-04 0
2016-04-09 0
2016-04-14 0
$`11141550000`
y
2016-02-05 18
2016-03-03 8
2016-07-16 10
2016-08-12 10
One chunk of my code is extremely slow (I know for operations are not efficient in R and should be avoided if you want to become more pro in this language). I coded before a chunk like this:
for (i in 1:length(lt)){
lt[[i]] <- lt[[i]][as.Date(index(lt[[i]]), format = "%Y-%m-%d") < "2018-11-01"]
}
I am trying to translate this for into a fast Filter or sapply, lapply operation, where every value of each list must be before the date "2018-11-01". Nevertheless, I have not been capable of:
1st attempt:
f <- function(i){
lt[[i]][as.Date(index(lt[[i]]), format = "%Y-%m-%d") < "2018-11-01"]
}
lapply(lt, function(x) f(x))
But received error:
Error in lt[[i]] : recursive indexing failed at level 2
2nd attempt:
f <- function(i){
lt[[i]][as.Date(index(lt[[i]]), format = "%Y-%m-%d") < "2018-11-01"]
}
Filter(function(x) f, lt)
But I received this message:
Error in Filter(function(x) f, lt) :
(list) object cannot be coerced to type 'logical'
3rd Attempt:
Filter(f, lt)
But again, I received an error:
Error in lt[[i]] : recursive indexing failed at level 2
I would appreciate any help in translating this for operation, as I need understanding better the dynamic of apply functions.
P.D.1.
Thanks to J.Gourlay feedback I am adding a sample of my list with dput:
> dput(lt)
structure(list(`11140780000` = structure(c(2, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 4, 0, 0, 0, 0, 0, 0, 1
), .Dim = c(132L, 1L), .Dimnames = list(NULL, "y"), index = structure(c(1458864000,
1459296000, 1459728000, 1460160000, 1460592000, 1461024000, 1461456000,
1461888000, 1462320000, 1462752000, 1463184000, 1463616000, 1464048000,
1464480000, 1464912000, 1465344000, 1465776000, 1466208000, 1466640000,
1467072000, 1467504000, 1467936000, 1468368000, 1468800000, 1469232000,
1469664000, 1470096000, 1470528000, 1470960000, 1471392000, 1471824000,
1472256000, 1472688000, 1473120000, 1473552000, 1473984000, 1474416000,
1474848000, 1475280000, 1475712000, 1476144000, 1476576000, 1477008000,
1477440000, 1477872000, 1478304000, 1478736000, 1479168000, 1479600000,
1480032000, 1480464000, 1480896000, 1481328000, 1481760000, 1482192000,
1482624000, 1483056000, 1483142400, 1483574400, 1484006400, 1484438400,
1484870400, 1485302400, 1485734400, 1486166400, 1486598400, 1487030400,
1487462400, 1487894400, 1488326400, 1488758400, 1489190400, 1489622400,
1490054400, 1490486400, 1490918400, 1491350400, 1491782400, 1492214400,
1492646400, 1493078400, 1493510400, 1493942400, 1494374400, 1494806400,
1495238400, 1495670400, 1496102400, 1496534400, 1496966400, 1497398400,
1497830400, 1498262400, 1498694400, 1499126400, 1499558400, 1499990400,
1500422400, 1500854400, 1501286400, 1501718400, 1502150400, 1502582400,
1503014400, 1503446400, 1503878400, 1504310400, 1504742400, 1505174400,
1505606400, 1506038400, 1506470400, 1506902400, 1507334400, 1507766400,
1508198400, 1508630400, 1509062400, 1509494400, 1509926400, 1510358400,
1510790400, 1511222400, 1511654400, 1512086400, 1512518400, 1512950400,
1513382400, 1513814400, 1514246400, 1514678400, 1514764800), tzone = "UTC", tclass = "Date"), .indexCLASS = "Date", .indexTZ = "UTC", tclass = "Date", tzone = "UTC", class = c("xts",
"zoo")), `11141550000` = structure(c(18, 8, 6, 0, 4, 8, 10, 10,
0, 23, 0, 8, 0, 2, 14, 16, 20, 4, 4, 4), .indexCLASS = "Date", .indexTZ = "UTC", tclass = "Date", tzone = "UTC", class = c("xts",
"zoo"), index = structure(c(1454630400, 1456963200, 1459296000,
1461628800, 1463961600, 1466294400, 1468627200, 1470960000, 1473292800,
1475625600, 1477958400, 1480291200, 1482624000, 1483142400, 1483833600,
1486166400, 1488499200, 1490832000, 1493164800, 1495497600), tzone = "UTC", tclass = "Date"), .Dim = c(20L,
1L), .Dimnames = list(NULL, "y"))), .Names = c("11140780000",
"11141550000"))
P.D.2.
As per suggestion of Jozef, I confirm that I have used library zoo to reproduce my code. I.e. function index of zoo package in the for loop.
Essentially, you are passing an object that expects an integer index in your lapply. Consider wrapping lt in seq_along(lt) for the input of lapply. Also, remember unlike for loops, apply functions return objects. So assign lapply to an object. Also, when function accepts one non-optional argument, there is no need for specifying the function operator.
f <- function(i){
lt[[i]][as.Date(index(lt[[i]]), format = "%Y-%m-%d") < "2018-11-01"]
}
new_lt <- lapply(seq_along(lt), f)
If your for loop works as you say, new_lt should be exactly the same as lt after running for loop:
all.equal(lt_after_for_loop, new_lt)
# [1] TRUE
identical(lt_after_for_loop, new_lt)
# [1] TRUE
Alternatively, pass whole objects instead of using indexing with adjusted defined function:
f <- function(obj){
obj[as.Date(index(obj), format = "%Y-%m-%d") < "2018-11-01"]
}
new_lt <- lapply(lt, f)
Finally, Filter() works to filter (keep or remove) a list's object by logical condition, not the contents within each item of list unless you are using such inner contents to decide the top level item to be removed from list. But for and lapply does not exclude items during processing (i.e., same number of items before/after their calls).

Improving for loop inception speed when comparing Date-time values

I am looking to speed up some code of mine, and am looking for some advice. I have two dataframes, and want to take data from one and input it into the other, but their formats are quite different. I have written some code that works, but it runs fairly slow, especially when I have large datasets. Example dataframes are below:
dat = structure(list(Date = structure(c(1508641200, 1508643000, 1508644800,
1508646600, 1508648400, 1508650200, 1508652000, 1508653800, 1508655600,
1508657400, 1508659200, 1508661000, 1508662800, 1508664600, 1508666400,
1508668200, 1508670000, 1508671800, 1508673600, 1508675400, 1508677200,
1508679000, 1508680800, 1508682600, 1508684400, 1508686200, 1508688000,
1508689800, 1508691600, 1508693400), class = c("POSIXct", "POSIXt"
), tzone = "EST"), X = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), tru = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("Date", "X", "tru"), row.names = c(NA,
30L), class = "data.frame")
and,
truth = structure(list(startdate = structure(c(1509937620, 1510705200,
1510722240, 1512245160, 1512250560, 1512251760, 1512271140, 1512274440,
1512984360, 1512986760, 1513002600, 1513004700, 1513752000, 1513753800,
1513775940, 1513776840, 1514268900, 1514289000, 1514307900, 1517842620,
1518056460, 1520031660, 1520050560, 1520075580, 1520218620), class =c("POSIXct",
"POSIXt"), tzone = ""), enddate = structure(c(1509952320, 1510718040,
1510737240, 1512250260, 1512269640, 1512269940, 1512290280, 1512289380,
1513001100, 1513001400, 1513023840, 1513021440, 1513772640, 1513771440,
1513797180, 1513796280, 1514288400, 1514307600, 1514311140, 1517857320,
1518067560, 1520049060, 1520073240, 1520078880, 1520222820), class =c("POSIXct",
"POSIXt"), tzone = ""), Y = c(42340.1667145732, 49765.2381579195,
48687.3848496384, 31170.9693454847, 50435.3541955455, 49757.5112973802,
44031.8550803252, 45912.1378875664, 47193.1529894274, 49826.4304479959,
45840.7120690243, 42483.44259103, 52188.4048476908, 52783.8164119854,
51769.0550080142, 49866.1301140174, 52250.5531316799, 49754.6933212176,
45256.185763228, 47742.0544890968, 53414.0366523465, 51881.9495162963,
48632.3656223053, 44476.3677890439, 61922.0098972212)), .Names =
c("startdate",
"enddate", "Y"), row.names = c(NA, 25L), class = "data.frame")
Now, what I want to do is take values from truth$Y and input them into dat$tru at their appropriate date-time values given as these actions happen between truth$startdate and truth$enddate.
What I have currently is the following code to do so:
for(i in 1:length(truth$startdate)){
for(j in 1:length(dat$Date)){
if(dat$Date[j] >= truth$startdate[i] && dat$Date[j] <= truth$enddate[i]){
dat$tru[j] = truth$Y[i]
}
}
}
Is there a more efficient way to do this avoiding a for loop inside of a for loop? Thanks in advance!
Using the between function from the dplyr package, you can eliminate the inner loop:
library(dplyr)
for(i in 1:length(truth$startdate)){
dat$tru[between(dat$Date, truth$startdate[i], truth$enddate[i])] <-truth$Y[i]
}
This should result in a significant performance improvement.
I sure this could be further refined but this is a quick and easy fix. I suggest looking at the purrr package, it could offer some additional performance improvements.

R apply funciton on each cell in data frame

I have a data frame that look something like this
> dput(tes)
structure(list(path = structure(1:6, .Label = c("1893-chicago-fair",
"1960s-afghanistan", "1970s-iran", "1980s-new-york", "20-bizarre-vintage-ads",
"20-bizarre-vintage-ads?utm_campaign=6678&utm_medium=rpages&utm_source=Facebook&utm_term=1e8e704f7b587515c72e6cf7895d55fd110b652c480d98c1440f0a7acba5fb0e",
"20-photos-segregation-america-show-far-weve-come-much-farther-go",
"7-bizarre-cultural-practices", "7-creepy-abandoned-cities?utm_campaign=6928&utm_medium=rpages&utm_source=Facebook&utm_term=4015a7368b588ff09694c96ba720c58f4e7f41a05b4181908b582bae682bef5e",
"a-brief-history-of-hippies", "abandoned-photographs", "albert-kahn",
"amazing-facts", "american-bison-extinction-1800s", "american-english-vs-british-english",
"andre-the-giant-photos", "andre-the-giant-photos??utm_source=facebook&sr_source=lift_facebook&utm_campaign=simplereach_andre-the-giant-photos&utm_medium=social",
"andre-the-giant-photos?grvVariant=d27feef0bfad84d60f335d3a8d241d9e",
"andre-the-giant-photos?grvVariant=d27feef0bfad84d60f335d3a8d241d9e&utm_campaign=gravityus2_142deb68f67fb1a99e7b80250fecc932&utm_medium=referral&utm_source=gravity",
"andre-the-giant-photos?grvVariant=d27feef0bfad84d60f335d3a8d241d9e&utm_campaign=gravityus2_16d63cf07ecf656f602b2d6b209344f7&utm_medium=referral&utm_source=gravity",
"andre-the-giant-photos?grvVariant=d27feef0bfad84d60f335d3a8d241d9e&utm_campaign=gravityus2_713050ecffc51540af02b2246ddf57dd&utm_medium=referral&utm_source=gravity",
"andre-the-giant-photos?grvVariant=d27feef0bfad84d60f335d3a8d241d9e&utm_campaign=gravityus2_c5bb3bc5e9408e0ad52ec9e787bd8654&utm_medium=referral&utm_source=gravity",
"andre-the-giant-photos?sr_source=lift_facebook&utm_campaign=simplereach_andre-the-giant-photos&utm_medium=social&utm_source=facebook",
"astounding-aerial-photography", "astounding-aerial-photography?utm_campaign=7002&utm_medium=rpages&utm_source=Facebook&utm_term=38e9e903d9ba59106d8b4d19be593f3de7ff8b91b12eafa03f2e382228f7b0d1",
"august-landmesser", "ben-franklin", "best-all-that-is-interesting-articles",
"bigfoot-facts", "celebrity-school-photos?grvVariant=82c0ce57a33dfd0209bdefc878665de0&utm_campaign=gravityus2_bc8646aefd6d0a16af03d7caf248f226&utm_medium=referral&utm_source=gravity",
"coolest-mushrooms?utm_campaign=taboolaINTL&utm_medium=referral&utm_source=taboola",
"craziest-ways-drugs-smuggled", "creepy-halloween-costumes",
"danakil-depression", "dark-john-lennon-quotes", "david-bowie-quotes",
"days-in-groundhog-day", "death-photos", "death-photos?utm_campaign=taboolaINTL&utm_medium=referral&utm_source=taboola",
"dr-seuss-quotes", "dream-chaser-spacecraft", "dust-bowl", "earth-two-planets",
"eixample-barcelona", "email-to-space", "evil-science-experiments",
"famous-incest", "famous-spies", "fun-facts-trivia", "golden-age-air-travel?utm_campaign=taboolaINTL&utm_medium=referral&utm_source=taboola",
"gross-foods", "gross-foods?utm_campaign=6928&utm_medium=rpages&utm_source=Facebook&utm_term=106965c54919c24bf37356500ec50f0709b1de621d6950bb4c5d48759ea3677e",
"gross-foods?utm_campaign=6928&utm_medium=rpages&utm_source=Facebook&utm_term=184e0ee39e66af82f9b124b904f6e07964b211e902cb0dc00c28771ff46163a2",
"gross-foods?utm_campaign=6928&utm_medium=rpages&utm_source=Facebook&utm_term=1a0ddea7bed770d5473c45e9f8d81dfd0c4fdd232f207c6b88b53c41ff220c59",
"gross-foods?utm_campaign=6928&utm_medium=rpages&utm_source=Facebook&utm_term=538659f1fc53f28d2c87b93ac73973681c1a46a04954964ab6c52ed1ab09b33a",
"gross-foods?utm_campaign=6928&utm_medium=rpages&utm_source=Facebook&utm_term=87caf0acb91ae2b202f1b00ad9eaad3fef20bbfb23405b9047fb2b5a5462ab9c",
"gross-foods?utm_campaign=6928&utm_medium=rpages&utm_source=Facebook&utm_term=91eae42c8fc9568103d46e0b6b6ec08fc34fd68b2e1918ffe2333ec73035c95a",
"gross-foods?utm_campaign=6928&utm_medium=rpages&utm_source=Facebook&utm_term=a72946874b2003a8e40635c6cf10c851d4e1c0ed45e645d69663214239550602",
"gross-foods?utm_campaign=6928&utm_medium=rpages&utm_source=Facebook&utm_term=ab594f0a1be002c8c3db297e8d33b04678af40e6a6469ac815884ae0a014b3a3",
"gross-foods?utm_campaign=6928&utm_medium=rpages&utm_source=Facebook&utm_term=fb1e333dd58cb7bb9251ec52290aae21771149f73e083440047068a69aaeae09",
"hilarious-insults", "hippie-communes", "hippie-communes?grvVariant=fda07538efb1c25617f7cc3d09c37c79",
"hippie-communes?grvVariant=fda07538efb1c25617f7cc3d09c37c79&utm_campaign=gravityus2_e3cd42d4745768460dab4694a972fd82&utm_medium=referral&utm_source=gravity",
"hippie-communes?pp=0", "history-of-the-vibrator", "history-of-the-vibrator?utm_campaign=whfbpd&utm_medium=social&utm_source=facebook",
"homosexuality-norm", "hunger-games-facts?utm_campaign=6905&utm_medium=rpages&utm_source=Facebook&utm_term=1a9e42ac8abb6ffa90bf0542206505e74d3df12114a2c4445527fb2b88ef8880",
"influential-photographs", "ingeniously-creative-ads", "insane-cults",
"insane-rulers", "inspirational-quotes", "inspirational-quotes?utm_medium=referral&utm_source=taboolainternal",
"interesting-facts-about-the-world", "interesting-quotes", "krokodil",
"making-a-murderer-theories", "maya-angelou-greatest-quotes",
"medieval-torture-devices", "milky-way-colorado", "montreal-metro",
"most-popular-female-names-in-america", "neil-degrasse-tyson-tweets",
"new-york-city-cinemagraphs", "new-york-subways-1980s", "north-korea-photographs",
"north-korea-photographs?utm_campaign=taboolaINTL&utm_medium=referral&utm_source=taboola",
"north-korea-photographs?utm_medium=referral&utm_source=taboolainternal",
"obama-aging", "pablo-escobar", "pablo-escobar??utm_source=facebook",
"pablo-escobar??utm_source=facebook&sr_source=lift_facebook&utm_campaign=simplereach_pablo-escobar&utm_medium=social",
"pablo-escobar?utm_campaign=whfbpd&utm_medium=social&utm_source=facebook",
"panda-facts", "photo-of-the-day-nasa-releases-crystal-clear-image-of-pluto",
"pollution-in-china-photographs", "pollution-in-china-photographs?utm_campaign=3434&utm_medium=rpages&utm_source=Facebook&utm_term=1a0ddea7bed770d5473c45e9f8d81dfd0c4fdd232f207c6b88b53c41ff220c59",
"pollution-in-china-photographs?utm_campaign=3434&utm_medium=rpages&utm_source=Facebook&utm_term=e28a76c1572c36c3a13965e52b4b2ea10518eb9f9c79c4bc84cfb85db16be81e",
"pollution-in-china-photographs?utm_campaign=6806&utm_medium=rpages&utm_source=Facebook&utm_term=1a0ddea7bed770d5473c45e9f8d81dfd0c4fdd232f207c6b88b53c41ff220c59",
"pollution-in-china-photographs?utm_campaign=7048&utm_medium=rpages&utm_source=Facebook&utm_term=2ef4bd7b6cd587601d6eeb35925282a1ed095ebbd4e9e4c0337ef868c7de7a0b",
"pollution-in-china-photographs?utm_campaign=7458&utm_medium=rpages&utm_source=Facebook&utm_term=b9e79a51cd4daf4c3ec02accce75b3e1fc9a22cb3133460c9c32a4f2f9cdb68c",
"powerful-photos-of-2014", "real-x-files", "romanovs-last-days",
"science-of-human-decay", "scientific-discoveries-2015", "scully-effect",
"serial-killer-quotes", "shah-iran", "six-of-the-craziest-gods-in-mythology",
"space-facts", "sun-facts", "sunken-cities", "sunken-ships",
"super-bowl-i-facts", "superhero-movies", "surreal-places", "syrian-civil-war-photographs",
"the-five-greatest-mysteries-of-human-history", "the-four-most-important-battles-of-ancient-greece",
"the-most-colorful-cities-in-the-world", "titanic-facts", "titanic-facts?utm_campaign=6385&utm_medium=rpages&utm_source=Facebook&utm_term=f5905e878216d14e20457ee3265caf6c10022d9545609edfb9a3cb0642c1a310",
"titanic-facts?utm_campaign=6899&utm_medium=rpages&utm_source=Facebook&utm_term=b9e79a51cd4daf4c3ec02accce75b3e1fc9a22cb3133460c9c32a4f2f9cdb68c",
"titanic-facts?utm_campaign=6928&utm_medium=rpages&utm_source=Facebook&utm_term=106965c54919c24bf37356500ec50f0709b1de621d6950bb4c5d48759ea3677e",
"titanic-facts?utm_campaign=6928&utm_medium=rpages&utm_source=Facebook&utm_term=538659f1fc53f28d2c87b93ac73973681c1a46a04954964ab6c52ed1ab09b33a",
"titanic-facts?utm_campaign=6928&utm_medium=rpages&utm_source=Facebook&utm_term=91eae42c8fc9568103d46e0b6b6ec08fc34fd68b2e1918ffe2333ec73035c95a",
"titanic-facts?utm_campaign=6928&utm_medium=rpages&utm_source=Facebook&utm_term=ab594f0a1be002c8c3db297e8d33b04678af40e6a6469ac815884ae0a014b3a3",
"titanic-facts?utm_campaign=6928&utm_medium=rpages&utm_source=Facebook&utm_term=d1864657a05e5b716bb5cb16a29f068a55652eb39fb669ea9c22a6486198f227",
"titanic-facts?utm_campaign=7292&utm_medium=rpages&utm_source=Facebook&utm_term=f5905e878216d14e20457ee3265caf6c10022d9545609edfb9a3cb0642c1a310",
"us-veterans-portraits", "vintage-disneyland", "wall-street-early-20th-century",
"what-we-love-this-week-the-incredible-last-words-of-famous-historical-figures",
"woodstock-photos", "zombie-proof-house"), class = "factor"),
`0089` = c(0, 0, 0, 0, 0, 1), `0096` = c(0, 0, 0, 0, 0, 0
), `02` = c(0, 0, 0, 0, 0, 0), `0215` = c(0, 0, 0, 0, 0,
0), `0225` = c(0, 0, 0, 0, 0, 0), `0252` = c(0, 0, 0, 0,
0, 0), `0271` = c(0, 0, 0, 0, 0, 0), `0272` = c(0, 0, 0,
0, 0, 0), `03` = c(0, 0, 0, 0, 1, 1)), .Names = c("path",
"0089", "0096", "02", "0215", "0225", "0252", "0271", "0272",
"03"), row.names = c(NA, 6L), class = "data.frame")
and I need to apply the min(x,1) function such that this function scan each value in the dataframe (except first column which is not numeric) and return the min(x,1). that way I have only zero's and one's.
I have tried:
f <- function(x) min(1,x)
res1<-do.call(f,tes[,2:ncol(tes)])
but that does not output the right result.
Any help aapreciated
We can use pmin
tes[,-1] <- pmin(1, as.matrix(tes[,-1]))
Or if we need only binary values
tes[,-1] <- +(!!tes[,-1])

Resources