r

in R: find all unique values in column separated by comma

六月ゝ 毕业季﹏ 提交于 2021-02-13 17:23:14
问题 I have multiple observations of one species with different observers / groups of observers and want to create a list of all unique observers. My data look like this: data <- read.table(text="species observer 1 A,B 1 A,B 1 B,E 1 B,E 1 D,E,A,C,C 1 F" , header = TRUE, stringsAsFactors = FALSE) My output should return a list of all unique observers - so: A,B,C,E,F I tried to substring the data in column C using the following command but that only returns the unique combinations of observers. all

Collapse / concatenate / aggregate multiple columns to a single comma separated string within each group

爱⌒轻易说出口 提交于 2021-02-13 17:07:17
问题 This is an extension to post Collapse / concatenate / aggregate a column to a single comma separated string within each group Goal: aggregate multiple columns according to one grouping variable and separate individual values by separator of choice. Reproducible example: data <- data.frame(A = c(rep(111, 3), rep(222, 3)), B = c(rep(c(100), 3), rep(200,3)), C = rep(c(1,2,NA),2), D = c(15:20), E = rep(c(1,NA,NA),2)) data A B C D E 1 111 100 1 15 1 2 111 100 2 16 NA 3 111 100 NA 17 NA 4 222 200 1

Collapse / concatenate / aggregate multiple columns to a single comma separated string within each group

最后都变了- 提交于 2021-02-13 17:06:23
问题 This is an extension to post Collapse / concatenate / aggregate a column to a single comma separated string within each group Goal: aggregate multiple columns according to one grouping variable and separate individual values by separator of choice. Reproducible example: data <- data.frame(A = c(rep(111, 3), rep(222, 3)), B = c(rep(c(100), 3), rep(200,3)), C = rep(c(1,2,NA),2), D = c(15:20), E = rep(c(1,NA,NA),2)) data A B C D E 1 111 100 1 15 1 2 111 100 2 16 NA 3 111 100 NA 17 NA 4 222 200 1

Collapse / concatenate / aggregate multiple columns to a single comma separated string within each group

不打扰是莪最后的温柔 提交于 2021-02-13 17:05:33
问题 This is an extension to post Collapse / concatenate / aggregate a column to a single comma separated string within each group Goal: aggregate multiple columns according to one grouping variable and separate individual values by separator of choice. Reproducible example: data <- data.frame(A = c(rep(111, 3), rep(222, 3)), B = c(rep(c(100), 3), rep(200,3)), C = rep(c(1,2,NA),2), D = c(15:20), E = rep(c(1,NA,NA),2)) data A B C D E 1 111 100 1 15 1 2 111 100 2 16 NA 3 111 100 NA 17 NA 4 222 200 1

Collapse / concatenate / aggregate multiple columns to a single comma separated string within each group

╄→гoц情女王★ 提交于 2021-02-13 17:04:11
问题 This is an extension to post Collapse / concatenate / aggregate a column to a single comma separated string within each group Goal: aggregate multiple columns according to one grouping variable and separate individual values by separator of choice. Reproducible example: data <- data.frame(A = c(rep(111, 3), rep(222, 3)), B = c(rep(c(100), 3), rep(200,3)), C = rep(c(1,2,NA),2), D = c(15:20), E = rep(c(1,NA,NA),2)) data A B C D E 1 111 100 1 15 1 2 111 100 2 16 NA 3 111 100 NA 17 NA 4 222 200 1

Add new value to new column based on if value exists in other dataframe in R

主宰稳场 提交于 2021-02-13 05:38:47
问题 I have two dataframes called users and purchases with thousands of datasets to each. Both have a feature called ID . My aim is to add a new column called buyer to the dataframe purchases , if the value of ID of purchases exists in ID of users . So the two dataframes look like this: users = data.frame("ID" = c(23432,75645,5465645,5656,6456)) purchases = data.frame("ID" = c(6456,4436,88945)) It should look like: 回答1: We can use %in% to compare the values and wrap as.integer to convert logical

extract part of word into a field from a long string using R

旧巷老猫 提交于 2021-02-12 11:41:18
问题 I have a single long string variable with 3 obs. I was trying to create a field prob to extract the specific string from the long string. the code and message is below. data aa: "The probability of being a carrier is 0.0002422359 " " an BRCA1 carrier 0.0001061067 " " an BRCA2 carrier 0.00013612 " enter code here aa$prob <- ifelse(grepl("The probability of being a carrier is", xx)==TRUE, word(aa, 8, 8), ifelse(grepl("BRCA", xx)==TRUE, word(aa, 5, 5), NA)) Warning message: In aa$prob <- ifelse

Identifying points by color

ぐ巨炮叔叔 提交于 2021-02-12 11:40:21
问题 I am following the tutorial over here : https://www.rpubs.com/loveb/som . This tutorial shows how to use the Kohonen Network (also called SOM, a type of machine learning algorithm) on the iris data. I ran this code from the tutorial: library(kohonen) #fitting SOMs library(ggplot2) #plots library(GGally) #plots library(RColorBrewer) #colors, using predefined palettes iris_complete <-iris[complete.cases(iris),] iris_unique <- unique(iris_complete) # Remove duplicates #scale data iris.sc = scale

extract part of word into a field from a long string using R

人盡茶涼 提交于 2021-02-12 11:40:11
问题 I have a single long string variable with 3 obs. I was trying to create a field prob to extract the specific string from the long string. the code and message is below. data aa: "The probability of being a carrier is 0.0002422359 " " an BRCA1 carrier 0.0001061067 " " an BRCA2 carrier 0.00013612 " enter code here aa$prob <- ifelse(grepl("The probability of being a carrier is", xx)==TRUE, word(aa, 8, 8), ifelse(grepl("BRCA", xx)==TRUE, word(aa, 5, 5), NA)) Warning message: In aa$prob <- ifelse

Identifying points by color

不想你离开。 提交于 2021-02-12 11:39:41
问题 I am following the tutorial over here : https://www.rpubs.com/loveb/som . This tutorial shows how to use the Kohonen Network (also called SOM, a type of machine learning algorithm) on the iris data. I ran this code from the tutorial: library(kohonen) #fitting SOMs library(ggplot2) #plots library(GGally) #plots library(RColorBrewer) #colors, using predefined palettes iris_complete <-iris[complete.cases(iris),] iris_unique <- unique(iris_complete) # Remove duplicates #scale data iris.sc = scale