我试图弄清楚如何组合多个列,不包括NA值。
输入数据帧:
data <- data.frame(
id = c(1:3),
Item1 = c("Egg", "", ""),
Item2 = c("Chicken", "Flour", ""),
Item3 = c("", "", "Bread"),
Item4 = c("", "Milk", "")
)
所需数据框:
desired <- data.frame(
id = c(1:3),
Item1 = c("Egg", "", ""),
Item2 = c("Chicken", "Flour", ""),
Item3 = c("", "", "Bread"),
Item4 = c("", "Milk", ""),
Combine = c("Egg, Chicken", "Flour, Milk", "Bread")
)
我尝试使用以下代码组合这些值:
data$Combine = paste(data$Item1, data$Item2, data$Item3, data$Item4, sep=",")
问题在于我得到的结果如下:
蛋,鸡,,
,面粉,,牛奶
,面包,
答案 0 :(得分:0)
如果我们使用与OP帖子中类似的方法,则使用""
替换带有,
的前导/滞后空格,将,
重复gsub
的{{1}}替换为data$Combine <- gsub(",{2,}", ",",
gsub("^,+|,+$", "", do.call(paste, c(data[-1], sep=","))))
data$Combine
#[1] "Egg,Chicken" "Flour,Milk" "Bread"
1}}
paste
或另一种选择是使用trimws
,删除前导/滞后空格(\\s+
),然后使用,
替换一个或多个空格(gsub
) gsub("\\s+", ",", trimws(do.call(paste, data[-1])))
#[1] "Egg,Chicken" "Flour,Milk" "Bread"
data <- structure(list(ID = 1:3, Item1 = c("Egg", "", ""), Item2 = c("Chicken",
"Flour", ""), Item3 = c("", "", "Bread"), Item4 = c("", "Milk",
"")), .Names = c("ID", "Item1", "Item2", "Item3", "Item4"),
class = "data.frame", row.names = c(NA, -3L))
apply plugin: 'base'
apply plugin: 'maven-publish'
group = 'foo.bar'
version = '1.0'
task copyPrebuilt(type: Copy) {
from(projectDir) {
include 'some.jar'
rename 'some', "some-$version"
}
into buildDir
}
publishToMavenLocal.dependsOn copyPrebuilt
publishing {
publications {
prebuilt(MavenPublication) {
artifact file("$buildDir/some-${version}.jar")
}
}
}
答案 1 :(得分:0)
添加&#34;,&#34;对于非空值。
def scrape1(self):
html = self.browser.page_source
soup = BeautifulSoup(html, 'html.parser')
# add links to list for later use
urls = []
for videos in soup.find_all('a', {'class': 'watch-now'}):
links = videos['href']
urls.append(links)
return urls
def use(self):
urls = scrape1()
创建新列。
data1 <- sapply(data[-1], function(x) ifelse(x != "", paste(x, " ", sep = ","), ""))
data1 <- data.frame(id = c(1:3), data1)
剪掉最后的符号。
data$Combine <- paste0(data1$Item1, data1$Item2, data1$Item3, data1$Item4)